home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 / Aminet - June 1993 [Walnut Creek].iso / ab20 / languags / cpp / gcgp1409.lha / src / gcc-1.40 / cccp.c next >
Encoding:
C/C++ Source or Header  |  1992-01-27  |  147.9 KB  |  5,834 lines

  1. /* C Compatible Compiler Preprocessor (CCCP)
  2. Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
  3.                     Written by Paul Rubin, June 1986
  4.             Adapted to ANSI C, Richard Stallman, Jan 1987
  5.  
  6. This program is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 1, or (at your option) any
  9. later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.  In other words, you are welcome to use, share and improve this program.
  21.  You are forbidden to forbid anyone else to use, share and improve
  22.  what you give them.   Help stamp out software-hoarding!  */
  23.  
  24. typedef unsigned char U_CHAR;
  25.  
  26. #ifdef EMACS
  27. #define NO_SHORTNAMES
  28. #include "../src/config.h"
  29. #ifdef open
  30. #undef open
  31. #undef read
  32. #undef write
  33. #endif /* open */
  34. #endif /* EMACS */
  35.  
  36. #ifndef EMACS
  37. #include "config.h"
  38. #endif /* not EMACS */
  39.  
  40. #ifdef amigados
  41. /* since cpp uses alloca to store all its read files, this is quite deadly
  42.  * on a system with non-automatic stackgrowth like the amiga, so we better
  43.  * turn it off now.. */
  44. #undef alloca
  45. #endif
  46.  
  47. #ifndef CC_INCLUDE_DIR
  48. #define CC_INCLUDE_DIR "/usr/include"
  49. #endif
  50.  
  51. #ifndef STDC_VALUE
  52. #define STDC_VALUE 1
  53. #endif
  54.  
  55. /* In case config.h defines these.  */
  56. #undef bcopy
  57. #undef bzero
  58. #undef bcmp
  59.  
  60. #include <sys/types.h>
  61. #include <sys/stat.h>
  62. #include <ctype.h>
  63. #include <stdio.h>
  64. #include <signal.h>
  65.  
  66. #ifndef VMS
  67. #include <sys/file.h>
  68. #ifndef USG
  69. #include <sys/time.h>        /* for __DATE__ and __TIME__ */
  70. #include <sys/resource.h>
  71. #else
  72. #define index strchr
  73. #define rindex strrchr
  74. #include <time.h>
  75. #include <fcntl.h>
  76. #endif /* USG */
  77. #endif /* not VMS */
  78.  
  79. #ifndef __STDC__
  80. extern char *sys_errlist[];
  81. #define strerror(err) sys_errlist[err]
  82. #endif
  83.   
  84. /* VMS-specific definitions */
  85. #ifdef VMS
  86. #include <time.h>
  87. #include <errno.h>        /* This defines "errno" properly */
  88. #include <perror.h>        /* This defines sys_errlist/sys_nerr properly */
  89. #define O_RDONLY    0    /* Open arg for Read/Only  */
  90. #define O_WRONLY    1    /* Open arg for Write/Only */
  91. #define read(fd,buf,size)    VAX11_C_read(fd,buf,size)
  92. #define write(fd,buf,size)    VAX11_C_write(fd,buf,size)
  93. #ifdef __GNUC__
  94. #define BSTRING            /* VMS/GCC supplies the bstring routines */
  95. #endif /* __GNUC__ */
  96. #endif /* VMS */
  97.  
  98. #ifndef O_RDONLY
  99. #define O_RDONLY 0
  100. #endif 
  101.  
  102. #define max(a,b) ((a) > (b) ? (a) : (b))
  103.  
  104. #ifndef S_ISREG
  105. #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
  106. #endif
  107.  
  108. /* External declarations.  */
  109.  
  110. void bcopy (), bzero ();
  111. int bcmp ();
  112. extern char *getenv ();
  113. extern char *version_string;
  114.  
  115. /* Forward declarations.  */
  116.  
  117. struct directive;
  118. struct file_buf;
  119. struct arglist;
  120. struct argdata;
  121.  
  122. int do_define (), do_line (), do_include (), do_undef (), do_error (),
  123.   do_pragma (), do_if (), do_xifdef (), do_else (),
  124.   do_elif (), do_endif (), do_sccs (), do_once ();
  125.  
  126. struct hashnode *install ();
  127. struct hashnode *lookup ();
  128.  
  129. char *xmalloc (), *xrealloc (), *xcalloc (), *savestring ();
  130. void fatal (), fancy_abort (), pfatal_with_name (), perror_with_name ();
  131.  
  132. void macroexpand ();
  133. void dump_all_macros ();
  134. void conditional_skip ();
  135. void skip_if_group ();
  136. void output_line_command ();
  137. /* Last arg to output_line_command.  */
  138. enum file_change_code {same_file, enter_file, leave_file};
  139.  
  140. int grow_outbuf ();
  141. int handle_directive ();
  142. void memory_full ();
  143.  
  144. U_CHAR *macarg1 ();
  145. char *macarg ();
  146.  
  147. U_CHAR *skip_to_end_of_comment ();
  148. U_CHAR *skip_quoted_string ();
  149.  
  150. #ifndef FATAL_EXIT_CODE
  151. #define FATAL_EXIT_CODE 33    /* gnu cc command understands this */
  152. #endif
  153.  
  154. #ifndef SUCCESS_EXIT_CODE
  155. #define SUCCESS_EXIT_CODE 0    /* 0 means success on Unix.  */
  156. #endif
  157.  
  158. /* Name under which this program was invoked.  */
  159.  
  160. char *progname;
  161.  
  162. /* Nonzero means handle C++ comment syntax and use
  163.    extra default include directories for C++.  */
  164.  
  165. int cplusplus;
  166.  
  167. /* Current maximum length of directory names in the search path
  168.    for include files.  (Altered as we get more of them.)  */
  169.  
  170. int max_include_len;
  171.  
  172. /* Nonzero means copy comments into the output file.  */
  173.  
  174. int put_out_comments = 0;
  175.  
  176. /* Nonzero means don't process the ANSI trigraph sequences.  */
  177.  
  178. int no_trigraphs = 0;
  179.  
  180. /* Nonzero means print the names of included files rather than
  181.    the preprocessed output.  1 means just the #include "...",
  182.    2 means #include <...> as well.  */
  183.  
  184. int print_deps = 0;
  185.  
  186. /* Nonzero means don't output line number information.  */
  187.  
  188. int no_line_commands;
  189.  
  190. /* Nonzero means inhibit output of the preprocessed text
  191.    and instead output the definitions of all user-defined macros
  192.    in a form suitable for use as input to cccp.  */
  193.  
  194. int dump_macros;
  195.  
  196. /* Nonzero means give all the error messages the ANSI standard requires.  */
  197.  
  198. int pedantic;
  199.  
  200. /* Nonzero means don't print warning messages.  -w.  */
  201.  
  202. int inhibit_warnings = 0;
  203.  
  204. /* Nonzero means warn if slash-star appears in a comment.  */
  205.  
  206. int warn_comments;
  207.  
  208. /* Nonzero means warn if there are any trigraphs.  */
  209.  
  210. int warn_trigraphs;
  211.  
  212. /* Nonzero means try to imitate old fashioned non-ANSI preprocessor.  */
  213.  
  214. int traditional;
  215.  
  216. /* Nonzero causes output not to be done,
  217.    but directives such as #define that have side effects
  218.    are still obeyed.  */
  219.  
  220. int no_output;
  221.  
  222. /* I/O buffer structure.
  223.    The `fname' field is nonzero for source files and #include files
  224.    and for the dummy text used for -D and -U.
  225.    It is zero for rescanning results of macro expansion
  226.    and for expanding macro arguments.  */
  227. #define INPUT_STACK_MAX 200
  228. struct file_buf {
  229.   char *fname;
  230.   int lineno;
  231.   int length;
  232.   U_CHAR *buf;
  233.   U_CHAR *bufp;
  234.   /* Macro that this level is the expansion of.
  235.      Included so that we can reenable the macro
  236.      at the end of this level.  */
  237.   struct hashnode *macro;
  238.   /* Value of if_stack at start of this file.
  239.      Used to prohibit unmatched #endif (etc) in an include file.  */
  240.   struct if_stack *if_stack;
  241.   /* Object to be freed at end of input at this level.  */
  242.   U_CHAR *free_ptr;
  243. } instack[INPUT_STACK_MAX];
  244.  
  245. /* Current nesting level of input sources.
  246.    `instack[indepth]' is the level currently being read.  */
  247. int indepth = -1;
  248. #define CHECK_DEPTH(code) \
  249.   if (indepth >= (INPUT_STACK_MAX - 1))                    \
  250.     {                                    \
  251.       error_with_line (line_for_error (instack[indepth].lineno),    \
  252.                "macro or #include recursion too deep");        \
  253.       code;                                \
  254.     }
  255.  
  256. /* Current depth in #include directives that use <...>.  */
  257. int system_include_depth = 0;
  258.  
  259. typedef struct file_buf FILE_BUF;
  260.  
  261. /* The output buffer.  Its LENGTH field is the amount of room allocated
  262.    for the buffer, not the number of chars actually present.  To get
  263.    that, subtract outbuf.buf from outbuf.bufp. */
  264.  
  265. #define OUTBUF_SIZE 10    /* initial size of output buffer */
  266. FILE_BUF outbuf;
  267.  
  268. /* Grow output buffer OBUF points at
  269.    so it can hold at least NEEDED more chars.  */
  270.  
  271. #define check_expand(OBUF, NEEDED)  \
  272.   (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED))   \
  273.    ? grow_outbuf ((OBUF), (NEEDED)) : 0)
  274.  
  275. struct file_name_list
  276.   {
  277.     struct file_name_list *next;
  278.     char *fname;
  279.   };
  280.  
  281. /* #include "file" looks in source file dir, then stack. */
  282. /* #include <file> just looks in the stack. */
  283. /* -I directories are added to the end, then the defaults are added. */
  284. struct file_name_list include_defaults[] =
  285.   {
  286. #ifndef VMS
  287.     { &include_defaults[1], GCC_INCLUDE_DIR },
  288.     { &include_defaults[2], CC_INCLUDE_DIR },
  289.     { 0, "/usr/local/include" }
  290. #else
  291.     { &include_defaults[1], "GNU_CC_INCLUDE:" },       /* GNU includes */
  292.     { &include_defaults[2], "SYS$SYSROOT:[SYSLIB.]" }, /* VAX-11 "C" includes */
  293.     { 0, "." }    /* This makes normal VMS filespecs work OK. The "." forces */
  294.         /* the file through hack_vms_include_specification */
  295. #endif /* VMS */
  296.   };
  297.  
  298. /* These are used instead of the above, for C++.  */
  299. struct file_name_list cplusplus_include_defaults[] =
  300.   {
  301. #ifndef VMS
  302.     /* Pick up GNU C++ specific include files.  */
  303.     { &cplusplus_include_defaults[1], GPLUSPLUS_INCLUDE_DIR },
  304.     /* Use GNU CC specific header files.  */
  305.     { &cplusplus_include_defaults[2], GCC_INCLUDE_DIR },
  306.     { 0, CC_INCLUDE_DIR }
  307. #else
  308.     { &cplusplus_include_defaults[1], "GNU_GXX_INCLUDE:" },
  309.     { &cplusplus_include_defaults[2], "GNU_CC_INCLUDE:" },
  310.     /* VAX-11 C includes */
  311.     { &cplusplus_include_defaults[3], "SYS$SYSROOT:[SYSLIB.]" },
  312.     { 0, "." }    /* This makes normal VMS filespecs work OK */
  313. #endif /* VMS */
  314.   };
  315.  
  316. struct file_name_list *include = 0;    /* First dir to search */
  317.     /* First dir to search for <file> */
  318. struct file_name_list *first_bracket_include = 0;
  319. struct file_name_list *last_include = 0;    /* Last in chain */
  320.  
  321. /* List of included files that contained #once.  */
  322. struct file_name_list *dont_repeat_files = 0;
  323.  
  324. /* List of other included files.  */
  325. struct file_name_list *all_include_files = 0;
  326.  
  327. /* Structure allocated for every #define.  For a simple replacement
  328.    such as
  329.        #define foo bar ,
  330.    nargs = -1, the `pattern' list is null, and the expansion is just
  331.    the replacement text.  Nargs = 0 means a functionlike macro with no args,
  332.    e.g.,
  333.        #define getchar() getc (stdin) .
  334.    When there are args, the expansion is the replacement text with the
  335.    args squashed out, and the reflist is a list describing how to
  336.    build the output from the input: e.g., "3 chars, then the 1st arg,
  337.    then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
  338.    The chars here come from the expansion.  Whatever is left of the
  339.    expansion after the last arg-occurrence is copied after that arg.
  340.    Note that the reflist can be arbitrarily long---
  341.    its length depends on the number of times the arguments appear in
  342.    the replacement text, not how many args there are.  Example:
  343.    #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
  344.    pattern list
  345.      { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
  346.    where (x, y) means (nchars, argno). */
  347.  
  348. typedef struct definition DEFINITION;
  349. struct definition {
  350.   int nargs;
  351.   int length;            /* length of expansion string */
  352.   U_CHAR *expansion;
  353.   struct reflist {
  354.     struct reflist *next;
  355.     char stringify;        /* nonzero if this arg was preceded by a
  356.                    # operator. */
  357.     char raw_before;        /* Nonzero if a ## operator before arg. */
  358.     char raw_after;        /* Nonzero if a ## operator after arg. */
  359.     int nchars;            /* Number of literal chars to copy before
  360.                    this arg occurrence.  */
  361.     int argno;            /* Number of arg to substitute (origin-0) */
  362.   } *pattern;
  363.   /* Names of macro args, concatenated in reverse order
  364.      with comma-space between them.
  365.      The only use of this is that we warn on redefinition
  366.      if this differs between the old and new definitions.  */
  367.   U_CHAR *argnames;
  368. };
  369.  
  370. /* different kinds of things that can appear in the value field
  371.    of a hash node.  Actually, this may be useless now. */
  372. union hashval {
  373.   int ival;
  374.   char *cpval;
  375.   DEFINITION *defn;
  376. };
  377.  
  378.  
  379. /* The structure of a node in the hash table.  The hash table
  380.    has entries for all tokens defined by #define commands (type T_MACRO),
  381.    plus some special tokens like __LINE__ (these each have their own
  382.    type, and the appropriate code is run when that type of node is seen.
  383.    It does not contain control words like "#define", which are recognized
  384.    by a separate piece of code. */
  385.  
  386. /* different flavors of hash nodes --- also used in keyword table */
  387. enum node_type {
  388.  T_DEFINE = 1,    /* the `#define' keyword */
  389.  T_INCLUDE,    /* the `#include' keyword */
  390.  T_IFDEF,    /* the `#ifdef' keyword */
  391.  T_IFNDEF,    /* the `#ifndef' keyword */
  392.  T_IF,        /* the `#if' keyword */
  393.  T_ELSE,    /* `#else' */
  394.  T_PRAGMA,    /* `#pragma' */
  395.  T_ELIF,    /* `#else' */
  396.  T_UNDEF,    /* `#undef' */
  397.  T_LINE,    /* `#line' */
  398.  T_ERROR,    /* `#error' */
  399.  T_ENDIF,    /* `#endif' */
  400.  T_SCCS,    /* `#sccs', used on system V.  */
  401.  T_IDENT,    /* `#ident', used on system V.  */
  402.  T_SPECLINE,    /* special symbol `__LINE__' */
  403.  T_DATE,    /* `__DATE__' */
  404.  T_FILE,    /* `__FILE__' */
  405.  T_BASE_FILE,    /* `__BASE_FILE__' */
  406.  T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
  407.  T_VERSION,    /* `__VERSION__' */
  408.  T_TIME,    /* `__TIME__' */
  409.  T_CONST,    /* Constant value, used by `__STDC__' */
  410.  T_MACRO,    /* macro defined by `#define' */
  411.  T_DISABLED,    /* macro temporarily turned off for rescan */
  412.  T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
  413.  T_UNUSED    /* Used for something not defined.  */
  414.  };
  415.  
  416. struct hashnode {
  417.   struct hashnode *next;    /* double links for easy deletion */
  418.   struct hashnode *prev;
  419.   struct hashnode **bucket_hdr;    /* also, a back pointer to this node's hash
  420.                    chain is kept, in case the node is the head
  421.                    of the chain and gets deleted. */
  422.   enum node_type type;        /* type of special token */
  423.   int length;            /* length of token, for quick comparison */
  424.   U_CHAR *name;            /* the actual name */
  425.   union hashval value;        /* pointer to expansion, or whatever */
  426. };
  427.  
  428. typedef struct hashnode HASHNODE;
  429.  
  430. /* Some definitions for the hash table.  The hash function MUST be
  431.    computed as shown in hashf () below.  That is because the rescan
  432.    loop computes the hash value `on the fly' for most tokens,
  433.    in order to avoid the overhead of a lot of procedure calls to
  434.    the hashf () function.  Hashf () only exists for the sake of
  435.    politeness, for use when speed isn't so important. */
  436.  
  437. #define HASHSIZE 1403
  438. HASHNODE *hashtab[HASHSIZE];
  439. #define HASHSTEP(old, c) ((old << 2) + c)
  440. #define MAKE_POS(v) (v & ~0x80000000) /* make number positive */
  441.  
  442. /* Symbols to predefine.  */
  443.  
  444. #ifdef CPP_PREDEFINES
  445. char *predefs = CPP_PREDEFINES;
  446. #else
  447. char *predefs = "";
  448. #endif
  449.  
  450. /* `struct directive' defines one #-directive, including how to handle it.  */
  451.  
  452. struct directive {
  453.   int length;            /* Length of name */
  454.   int (*func)();        /* Function to handle directive */
  455.   char *name;            /* Name of directive */
  456.   enum node_type type;        /* Code which describes which directive. */
  457.   char angle_brackets;        /* Nonzero => <...> is special.  */
  458.   char traditional_comments;    /* Nonzero: keep comments if -traditional.  */
  459.   char pass_thru;        /* Copy preprocessed directive to output file.  */
  460. };
  461.  
  462. /* Here is the actual list of #-directives, most-often-used first.  */
  463.  
  464. struct directive directive_table[] = {
  465.   {  6, do_define, "define", T_DEFINE, 0, 1},
  466.   {  2, do_if, "if", T_IF},
  467.   {  5, do_xifdef, "ifdef", T_IFDEF},
  468.   {  6, do_xifdef, "ifndef", T_IFNDEF},
  469.   {  5, do_endif, "endif", T_ENDIF},
  470.   {  4, do_else, "else", T_ELSE},
  471.   {  4, do_elif, "elif", T_ELIF},
  472.   {  4, do_line, "line", T_LINE},
  473.   {  7, do_include, "include", T_INCLUDE, 1},
  474.   {  5, do_undef, "undef", T_UNDEF},
  475.   {  5, do_error, "error", T_ERROR},
  476. #ifdef SCCS_DIRECTIVE
  477.   {  4, do_sccs, "sccs", T_SCCS},
  478. #endif
  479.   {  6, do_pragma, "pragma", T_PRAGMA, 0, 0, 1},
  480.   {  -1, 0, "", T_UNUSED},
  481. };
  482.  
  483. /* table to tell if char can be part of a C identifier. */
  484. U_CHAR is_idchar[256];
  485. /* table to tell if char can be first char of a c identifier. */
  486. U_CHAR is_idstart[256];
  487. /* table to tell if c is horizontal space.  */
  488. U_CHAR is_hor_space[256];
  489. /* table to tell if c is horizontal or vertical space.  */
  490. U_CHAR is_space[256];
  491.  
  492. #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
  493. #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
  494.   
  495. int errors = 0;            /* Error counter for exit code */
  496.  
  497. /* Zero means dollar signs are punctuation.
  498.    -$ stores 0; -traditional, stores 1.  Default is 1 for VMS, 0 otherwise.
  499.    This must be 0 for correct processing of this ANSI C program:
  500.     #define foo(a) #a
  501.     #define lose(b) foo(b)
  502.     #define test$
  503.     lose(test)    */
  504. #ifndef DOLLARS_IN_IDENTIFIERS
  505. #define DOLLARS_IN_IDENTIFIERS 0
  506. #endif
  507. int dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
  508.  
  509. FILE_BUF expand_to_temp_buffer ();
  510.  
  511. DEFINITION *collect_expansion ();
  512.  
  513. /* Stack of conditionals currently in progress
  514.    (including both successful and failing conditionals).  */
  515.  
  516. struct if_stack {
  517.   struct if_stack *next;    /* for chaining to the next stack frame */
  518.   char *fname;        /* copied from input when frame is made */
  519.   int lineno;            /* similarly */
  520.   int if_succeeded;        /* true if a leg of this if-group
  521.                     has been passed through rescan */
  522.   enum node_type type;        /* type of last directive seen in this group */
  523. };
  524. typedef struct if_stack IF_STACK_FRAME;
  525. IF_STACK_FRAME *if_stack = NULL;
  526.  
  527. /* Buffer of -M output.  */
  528.  
  529. char *deps_buffer;
  530.  
  531. /* Number of bytes allocated in above.  */
  532. int deps_allocated_size;
  533.  
  534. /* Number of bytes used.  */
  535. int deps_size;
  536.  
  537. /* Number of bytes since the last newline.  */
  538. int deps_column;
  539.  
  540. /* Nonzero means -I- has been seen,
  541.    so don't look for #include "foo" the source-file directory.  */
  542. int ignore_srcdir;
  543.  
  544. /* Handler for SIGPIPE.  */
  545.  
  546. static void
  547. pipe_closed ()
  548. {
  549.   fatal ("output pipe has been closed");
  550. }
  551.  
  552. int
  553. main (argc, argv)
  554.      int argc;
  555.      char **argv;
  556. {
  557.   int st_mode;
  558.   long st_size;
  559.   char *in_fname, *out_fname;
  560.   int f, i;
  561.   FILE_BUF *fp;
  562.   char **pend_files = (char **) xmalloc (argc * sizeof (char *));
  563.   char **pend_defs = (char **) xmalloc (argc * sizeof (char *));
  564.   char **pend_undefs = (char **) xmalloc (argc * sizeof (char *));
  565.   int inhibit_predefs = 0;
  566.   int no_standard_includes = 0;
  567.  
  568.   /* Non-0 means don't output the preprocessed program.  */
  569.   int inhibit_output = 0;
  570.  
  571.   /* Stream on which to print the dependency information.  */
  572.   FILE *deps_stream = 0;
  573.   /* Target-name to write with the dependency information.  */
  574.   char *deps_target = 0;
  575.  
  576. #ifdef RLIMIT_STACK
  577.   /* Get rid of any avoidable limit on stack size.  */
  578.   {
  579.     struct rlimit rlim;
  580.  
  581.     /* Set the stack limit huge so that alloca (particularly stringtab
  582.      * in dbxread.c) does not fail. */
  583.     getrlimit (RLIMIT_STACK, &rlim);
  584.     rlim.rlim_cur = rlim.rlim_max;
  585.     setrlimit (RLIMIT_STACK, &rlim);
  586.   }
  587. #endif /* RLIMIT_STACK defined */
  588.  
  589.   progname = argv[0];
  590. #ifdef VMS
  591.   {
  592.     /* Remove directories from PROGNAME.  */
  593.     char *s;
  594.     extern char *rindex ();
  595.  
  596.     progname = savestring (argv[0]);
  597.  
  598.     if (!(s = rindex (progname, ']')))
  599.       s = rindex (progname, ':');
  600.     if (s)
  601.       strcpy (progname, s+1);
  602.     if (s = rindex (progname, '.'))
  603.       *s = '\0';
  604.   }
  605. #endif
  606.  
  607.   in_fname = NULL;
  608.   out_fname = NULL;
  609.  
  610.   /* Initialize is_idchar to allow $.  */
  611.   dollars_in_ident = 1;
  612.   initialize_char_syntax ();
  613.   dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
  614.  
  615.   no_line_commands = 0;
  616.   no_trigraphs = 1;
  617.   dump_macros = 0;
  618.   no_output = 0;
  619.   cplusplus = 0;
  620. #ifdef CPLUSPLUS
  621.   cplusplus = 1;
  622. #endif
  623.  
  624.   signal (SIGPIPE, pipe_closed);
  625.  
  626. #ifndef VMS
  627.   max_include_len
  628.     = max (max (sizeof (GCC_INCLUDE_DIR),
  629.         sizeof (GPLUSPLUS_INCLUDE_DIR)),
  630.        sizeof ("/usr/include/CC"));
  631. #else /* VMS */
  632.   max_include_len
  633.     = sizeof("SYS$SYSROOT:[SYSLIB.]");
  634. #endif /* VMS */
  635.  
  636.   bzero (pend_files, argc * sizeof (char *));
  637.   bzero (pend_defs, argc * sizeof (char *));
  638.   bzero (pend_undefs, argc * sizeof (char *));
  639.  
  640.   /* Process switches and find input file name.  */
  641.  
  642.   for (i = 1; i < argc; i++) {
  643.     if (argv[i][0] != '-') {
  644.       if (out_fname != NULL)
  645.     fatal ("Usage: %s [switches] input output", argv[0]);
  646.       else if (in_fname != NULL)
  647.     out_fname = argv[i];
  648.       else
  649.     in_fname = argv[i];
  650.     } else {
  651.       switch (argv[i][1]) {
  652.  
  653.       case 'i':
  654.     if (argv[i][2] != 0)
  655.       pend_files[i] = argv[i] + 2;
  656.     else if (i + 1 == argc)
  657.       fatal ("Filename missing after -i option");
  658.     else
  659.       pend_files[i] = argv[i+1], i++;
  660.     break;
  661.  
  662.       case 'o':
  663.     if (out_fname != NULL)
  664.       fatal ("Output filename specified twice");
  665.     if (i + 1 == argc)
  666.       fatal ("Filename missing after -o option");
  667.     out_fname = argv[++i];
  668.     if (!strcmp (out_fname, "-"))
  669.       out_fname = "";
  670.     break;
  671.  
  672.       case 'p':
  673.     pedantic = 1;
  674.     break;
  675.  
  676.       case 't':
  677.     if (!strcmp (argv[i], "-traditional")) {
  678.       traditional = 1;
  679.       dollars_in_ident = 1;
  680.     } else if (!strcmp (argv[i], "-trigraphs")) {
  681.       no_trigraphs = 0;
  682.     }
  683.     break;
  684.  
  685.       case '+':
  686.     cplusplus = 1;
  687.     break;
  688.  
  689.       case 'w':
  690.     inhibit_warnings = 1;
  691.     break;
  692.  
  693.       case 'W':
  694.     if (!strcmp (argv[i], "-Wtrigraphs")) {
  695.       warn_trigraphs = 1;
  696.     }
  697.     if (!strcmp (argv[i], "-Wcomments"))
  698.       warn_comments = 1;
  699.     if (!strcmp (argv[i], "-Wcomment"))
  700.       warn_comments = 1;
  701.     if (!strcmp (argv[i], "-Wall")) {
  702.       warn_trigraphs = 1;
  703.       warn_comments = 1;
  704.     }
  705.     break;
  706.  
  707.       case 'M':
  708.     if (!strcmp (argv[i], "-M"))
  709.       print_deps = 2;
  710.     else if (!strcmp (argv[i], "-MM"))
  711.       print_deps = 1;
  712.     inhibit_output = 1;
  713.     break;
  714.  
  715.       case 'd':
  716.     dump_macros = 1;
  717.     no_output = 1;
  718.     break;
  719.  
  720.       case 'v':
  721.     fprintf (stderr, "GNU CPP version %s\n", version_string);
  722.     break;
  723.  
  724.       case 'D':
  725.     {
  726.       char *p, *p1;
  727.  
  728.       if (argv[i][2] != 0)
  729.         p = argv[i] + 2;
  730.       else if (i + 1 == argc)
  731.         fatal ("Macro name missing after -D option");
  732.       else
  733.         p = argv[++i];
  734.  
  735.       if ((p1 = (char *) index (p, '=')) != NULL)
  736.         *p1 = ' ';
  737.       pend_defs[i] = p;
  738.     }
  739.     break;
  740.  
  741.       case 'U':        /* JF #undef something */
  742.     if (argv[i][2] != 0)
  743.       pend_undefs[i] = argv[i] + 2;
  744.     else if (i + 1 == argc)
  745.       fatal ("Macro name missing after -U option");
  746.     else
  747.       pend_undefs[i] = argv[i+1], i++;
  748.     break;
  749.  
  750.       case 'C':
  751.     put_out_comments = 1;
  752.     break;
  753.  
  754.       case 'E':            /* -E comes from cc -E; ignore it.  */
  755.     break;
  756.  
  757.       case 'P':
  758.     no_line_commands = 1;
  759.     break;
  760.  
  761.       case '$':            /* Don't include $ in identifiers.  */
  762.     dollars_in_ident = 0;
  763.     break;
  764.  
  765.       case 'I':            /* Add directory to path for includes.  */
  766.     {
  767.       struct file_name_list *dirtmp;
  768.  
  769.       if (! ignore_srcdir && !strcmp (argv[i] + 2, "-"))
  770.         ignore_srcdir = 1;
  771.       else {
  772.         dirtmp = (struct file_name_list *)
  773.           xmalloc (sizeof (struct file_name_list));
  774.         dirtmp->next = 0;        /* New one goes on the end */
  775.         if (include == 0)
  776.           include = dirtmp;
  777.         else
  778.           last_include->next = dirtmp;
  779.         last_include = dirtmp;    /* Tail follows the last one */
  780.         if (argv[i][2] != 0)
  781.           dirtmp->fname = argv[i] + 2;
  782.         else if (i + 1 == argc)
  783.           fatal ("Directory name missing after -I option");
  784.         else
  785.           dirtmp->fname = argv[++i];
  786.         if (strlen (dirtmp->fname) > max_include_len)
  787.           max_include_len = strlen (dirtmp->fname);
  788.         if (ignore_srcdir && first_bracket_include == 0)
  789.           first_bracket_include = dirtmp;
  790.         }
  791.     }
  792.     break;
  793.  
  794.       case 'n':
  795.     /* -nostdinc causes no default include directories.
  796.        You must specify all include-file directories with -I.  */
  797.     no_standard_includes = 1;
  798.     break;
  799.  
  800.       case 'u':
  801.     /* Sun compiler passes undocumented switch "-undef".
  802.        Let's assume it means to inhibit the predefined symbols.  */
  803.     inhibit_predefs = 1;
  804.     break;
  805.  
  806.       case '\0': /* JF handle '-' as file name meaning stdin or stdout */
  807.     if (in_fname == NULL) {
  808.       in_fname = "";
  809.       break;
  810.     } else if (out_fname == NULL) {
  811.       out_fname = "";
  812.       break;
  813.     }    /* else fall through into error */
  814.  
  815.       default:
  816.     fatal ("Invalid option `%s'", argv[i]);
  817.       }
  818.     }
  819.   }
  820.  
  821.   /* Now that dollars_in_ident is known, initialize is_idchar.  */
  822.   initialize_char_syntax ();
  823.  
  824.   /* Install __LINE__, etc.  Must follow initialize_char_syntax
  825.      and option processing.  */
  826.   initialize_builtins ();
  827.  
  828.   /* Do standard #defines that identify processor type.  */
  829.  
  830.   if (!inhibit_predefs) {
  831.     char *p = (char *) alloca (strlen (predefs) + 1);
  832.     strcpy (p, predefs);
  833.     while (*p) {
  834.       char *q;
  835.       if (p[0] != '-' || p[1] != 'D')
  836.     abort ();
  837.       q = &p[2];
  838.       while (*p && *p != ' ') p++;
  839.       if (*p != 0)
  840.     *p++= 0;
  841.       make_definition (q);
  842.     }
  843.   }
  844.  
  845.   /* Do defines specified with -D.  */
  846.   for (i = 1; i < argc; i++)
  847.     if (pend_defs[i])
  848.       make_definition (pend_defs[i]);
  849.  
  850.   /* Do undefines specified with -U.  */
  851.   for (i = 1; i < argc; i++)
  852.     if (pend_undefs[i])
  853.       make_undef (pend_undefs[i]);
  854.  
  855.   /* Unless -fnostdinc,
  856.      tack on the standard include file dirs to the specified list */
  857.   if (!no_standard_includes) {
  858.     if (include == 0)
  859.       include = (cplusplus ? cplusplus_include_defaults : include_defaults);
  860.     else
  861.       last_include->next
  862.     = (cplusplus ? cplusplus_include_defaults : include_defaults);
  863.     /* Make sure the list for #include <...> also has the standard dirs.  */
  864.     if (ignore_srcdir && first_bracket_include == 0)
  865.       first_bracket_include
  866.     = (cplusplus ? cplusplus_include_defaults : include_defaults);
  867.   }
  868.  
  869.   /* Initialize output buffer */
  870.  
  871.   outbuf.buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
  872.   outbuf.bufp = outbuf.buf;
  873.   outbuf.length = OUTBUF_SIZE;
  874.  
  875.   /* Scan the -i files before the main input.
  876.      Much like #including them, but with no_output set
  877.      so that only their macro definitions matter.  */
  878.  
  879.   no_output++;
  880.   for (i = 1; i < argc; i++)
  881.     if (pend_files[i]) {
  882.       int fd = open (pend_files[i], O_RDONLY, 0666);
  883.       if (fd < 0) {
  884.     perror_with_name (pend_files[i]);
  885.     return FATAL_EXIT_CODE;
  886.       }
  887.       finclude (fd, pend_files[i], &outbuf);
  888.     }
  889.   no_output--;
  890.  
  891.   /* Create an input stack level for the main input file
  892.      and copy the entire contents of the file into it.  */
  893.  
  894.   fp = &instack[++indepth];
  895.  
  896.   /* JF check for stdin */
  897.   if (in_fname == NULL || *in_fname == 0) {
  898.     in_fname = "";
  899.     f = 0;
  900.   } else if ((f = open (in_fname, O_RDONLY, 0666)) < 0)
  901.     goto perror;
  902.  
  903.   /* Either of two environment variables can specify output of deps.
  904.      Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
  905.      where OUTPUT_FILE is the file to write deps info to
  906.      and DEPS_TARGET is the target to mention in the deps.  */
  907.  
  908.   if (print_deps == 0
  909.       && (getenv ("SUNPRO_DEPENDENCIES") != 0
  910.       || getenv ("DEPENDENCIES_OUTPUT") != 0))
  911.     {
  912.       char *spec = getenv ("DEPENDENCIES_OUTPUT");
  913.       char *s;
  914.       char *output_file;
  915.  
  916.       if (spec == 0)
  917.     {
  918.       spec = getenv ("SUNPRO_DEPENDENCIES");
  919.       print_deps = 2;
  920.     }
  921.       else
  922.     print_deps = 1;
  923.  
  924.       s = spec;
  925.       /* Find the space before the DEPS_TARGET, if there is one.  */
  926.       /* Don't use `index'; that causes trouble on USG.  */
  927.       while (*s != 0 && *s != ' ') s++;
  928.       if (*s != 0)
  929.     {
  930.       deps_target = s + 1;
  931.       output_file = (char *) xmalloc (s - spec + 1);
  932.       bcopy (spec, output_file, s - spec);
  933.       output_file[s - spec] = 0;
  934.     }
  935.       else
  936.     {
  937.       deps_target = 0;
  938.       output_file = spec;
  939.     }
  940.       
  941.       deps_stream = fopen (output_file, "a");
  942.       if (deps_stream == 0)
  943.     pfatal_with_name (output_file);
  944.     }
  945.   /* If the -M option was used, output the deps to standard output.  */
  946.   else if (print_deps)
  947.     deps_stream = stdout;
  948.  
  949.   /* For -M, print the expected object file name
  950.      as the target of this Make-rule.  */
  951.   if (print_deps) {
  952.     deps_allocated_size = 200;
  953.     deps_buffer = (char *) xmalloc (deps_allocated_size);
  954.     deps_buffer[0] = 0;
  955.     deps_size = 0;
  956.     deps_column = 0;
  957.  
  958.     if (deps_target) {
  959.       deps_output (deps_target, 0);
  960.       deps_output (":", 0);
  961.     } else if (*in_fname == 0)
  962.       deps_output ("-: ", 0);
  963.     else {
  964.       int len;
  965.       char *p = in_fname;
  966.       char *p1 = p;
  967.       /* Discard all directory prefixes from P.  */
  968.       while (*p1) {
  969.     if (*p1 == '/')
  970.       p = p1 + 1;
  971. #ifdef amigados
  972.     if (*p1 == ':')
  973.       p = p1 + 1;
  974. #endif
  975.     p1++;
  976.       }
  977.       /* Output P, but remove known suffixes.  */
  978.       len = strlen (p);
  979.       if (p[len - 2] == '.'
  980.       && (p[len - 1] == 'c' || p[len - 1] == 'C' || p[len - 1] == 'S'))
  981.     deps_output (p, len - 2);
  982.       else if (p[len - 3] == '.'
  983.            && p[len - 2] == 'c'
  984.            && p[len - 1] == 'c')
  985.     deps_output (p, len - 3);
  986.       else
  987.     deps_output (p, 0);
  988.       /* Supply our own suffix.  */
  989.       deps_output (".o : ", 0);
  990.       deps_output (in_fname, 0);
  991.       deps_output (" ", 0);
  992.     }
  993.   }
  994.  
  995.   file_size_and_mode (f, &st_mode, &st_size);
  996.   fp->fname = in_fname;
  997.   fp->lineno = 1;
  998.   /* JF all this is mine about reading pipes and ttys */
  999.   if (!S_ISREG (st_mode)) {
  1000.     /* Read input from a file that is not a normal disk file.
  1001.        We cannot preallocate a buffer with the correct size,
  1002.        so we must read in the file a piece at the time and make it bigger.  */
  1003.     int size;
  1004.     int bsize;
  1005.     int cnt;
  1006.     U_CHAR *bufp;
  1007.  
  1008.     bsize = 2000;
  1009.     size = 0;
  1010.     fp->buf = (U_CHAR *) xmalloc (bsize + 2);
  1011.     bufp = fp->buf;
  1012.     for (;;) {
  1013.       cnt = read (f, bufp, bsize - size);
  1014.       if (cnt < 0) goto perror;    /* error! */
  1015.       if (cnt == 0) break;    /* End of file */
  1016.       size += cnt;
  1017.       bufp += cnt;
  1018.       if (bsize == size) {    /* Buffer is full! */
  1019.         bsize *= 2;
  1020.         fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
  1021.     bufp = fp->buf + size;    /* May have moved */
  1022.       }
  1023.     }
  1024.     fp->length = size;
  1025.   } else {
  1026.     /* Read a file whose size we can determine in advance.
  1027.        For the sake of VMS, st_size is just an upper bound.  */
  1028.     long i;
  1029.     fp->length = 0;
  1030.     fp->buf = (U_CHAR *) xmalloc (st_size + 2);
  1031.  
  1032.     while (st_size > 0) {
  1033.       i = read (f, fp->buf + fp->length, st_size);
  1034.       if (i <= 0) {
  1035.         if (i == 0) break;
  1036.     goto perror;
  1037.       }
  1038.       fp->length += i;
  1039.       st_size -= i;
  1040.     }
  1041.   }
  1042.   fp->bufp = fp->buf;
  1043.   fp->if_stack = if_stack;
  1044.   
  1045.   /* Unless inhibited, convert trigraphs in the input.  */
  1046.  
  1047.   if (!no_trigraphs)
  1048.     trigraph_pcp (fp);
  1049.  
  1050.   /* Make sure data ends with a newline.  And put a null after it.  */
  1051.  
  1052.   if (fp->length > 0 && fp->buf[fp->length-1] != '\n')
  1053.     fp->buf[fp->length++] = '\n';
  1054.   fp->buf[fp->length] = '\0';
  1055.  
  1056.   /* Now that we know the input file is valid, open the output.  */
  1057.  
  1058.   if (!out_fname || !strcmp (out_fname, ""))
  1059.     out_fname = "stdout";
  1060.   else if (! freopen (out_fname, "w", stdout))
  1061.     pfatal_with_name (out_fname);
  1062.  
  1063.   output_line_command (fp, &outbuf, 0, same_file);
  1064.  
  1065.   /* Scan the input, processing macros and directives.  */
  1066.  
  1067.   rescan (&outbuf, 0);
  1068.  
  1069.   /* Now we have processed the entire input
  1070.      Write whichever kind of output has been requested.  */
  1071.  
  1072.  
  1073.   if (dump_macros)
  1074.     dump_all_macros ();
  1075.   else if (! inhibit_output && deps_stream != stdout) {
  1076.     if (write (fileno (stdout), outbuf.buf, outbuf.bufp - outbuf.buf) < 0)
  1077.       fatal ("I/O error on output");
  1078.   }
  1079.  
  1080.   if (print_deps) {
  1081.     fputs (deps_buffer, deps_stream);
  1082.     putc ('\n', deps_stream);
  1083.     if (deps_stream != stdout) {
  1084.       fclose (deps_stream);
  1085.       if (ferror (deps_stream))
  1086.     fatal ("I/O error on output");
  1087.     }
  1088.   }
  1089.  
  1090.   if (ferror (stdout))
  1091.     fatal ("I/O error on output");
  1092.  
  1093.   if (errors)
  1094.     exit (FATAL_EXIT_CODE);
  1095.   exit (SUCCESS_EXIT_CODE);
  1096.  
  1097.  perror:
  1098.   pfatal_with_name (in_fname);
  1099. }
  1100.  
  1101. /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
  1102.    before main CCCP processing.  Name `pcp' is also in honor of the
  1103.    drugs the trigraph designers must have been on.
  1104.  
  1105.    Using an extra pass through the buffer takes a little extra time,
  1106.    but is infinitely less hairy than trying to handle ??/" inside
  1107.    strings, etc. everywhere, and also makes sure that trigraphs are
  1108.    only translated in the top level of processing. */
  1109.  
  1110. trigraph_pcp (buf)
  1111.      FILE_BUF *buf;
  1112. {
  1113.   register U_CHAR c, *fptr, *bptr, *sptr;
  1114.   int len;
  1115.  
  1116.   fptr = bptr = sptr = buf->buf;
  1117.   while ((sptr = (U_CHAR *) index (sptr, '?')) != NULL) {
  1118.     if (*++sptr != '?')
  1119.       continue;
  1120.     switch (*++sptr) {
  1121.       case '=':
  1122.       c = '#';
  1123.       break;
  1124.     case '(':
  1125.       c = '[';
  1126.       break;
  1127.     case '/':
  1128.       c = '\\';
  1129.       break;
  1130.     case ')':
  1131.       c = ']';
  1132.       break;
  1133.     case '\'':
  1134.       c = '^';
  1135.       break;
  1136.     case '<':
  1137.       c = '{';
  1138.       break;
  1139.     case '!':
  1140.       c = '|';
  1141.       break;
  1142.     case '>':
  1143.       c = '}';
  1144.       break;
  1145.     case '-':
  1146.       c  = '~';
  1147.       break;
  1148.     case '?':
  1149.       sptr--;
  1150.       continue;
  1151.     default:
  1152.       continue;
  1153.     }
  1154.     len = sptr - fptr - 2;
  1155.     if (bptr != fptr && len > 0)
  1156.       bcopy (fptr, bptr, len);    /* BSD doc says bcopy () works right
  1157.                    for overlapping strings.  In ANSI
  1158.                    C, this will be memmove (). */
  1159.     bptr += len;
  1160.     *bptr++ = c;
  1161.     fptr = ++sptr;
  1162.   }
  1163.   len = buf->length - (fptr - buf->buf);
  1164.   if (bptr != fptr && len > 0)
  1165.     bcopy (fptr, bptr, len);
  1166.   buf->length -= fptr - bptr;
  1167.   buf->buf[buf->length] = '\0';
  1168.   if (warn_trigraphs && fptr != bptr)
  1169.     warning ("%d trigraph(s) encountered", (fptr - bptr) / 2);
  1170. }
  1171.  
  1172. /* Move all backslash-newline pairs out of embarrassing places.
  1173.    Exchange all such pairs following BP
  1174.    with any potentially-embarrasing characters that follow them.
  1175.    Potentially-embarrassing characters are / and *
  1176.    (because a backslash-newline inside a comment delimiter
  1177.    would cause it not to be recognized).  */
  1178.  
  1179. newline_fix (bp)
  1180.      U_CHAR *bp;
  1181. {
  1182.   register U_CHAR *p = bp;
  1183.   register int count = 0;
  1184.  
  1185.   /* First count the backslash-newline pairs here.  */
  1186.  
  1187.   while (*p++ == '\\' && *p++ == '\n')
  1188.     count++;
  1189.  
  1190.   p = bp + count * 2;
  1191.  
  1192.   /* Exit if what follows the backslash-newlines is not embarrassing.  */
  1193.  
  1194.   if (count == 0 || (*p != '/' && *p != '*'))
  1195.     return;
  1196.  
  1197.   /* Copy all potentially embarrassing characters
  1198.      that follow the backslash-newline pairs
  1199.      down to where the pairs originally started.  */
  1200.  
  1201.   while (*p == '*' || *p == '/')
  1202.     *bp++ = *p++;
  1203.  
  1204.   /* Now write the same number of pairs after the embarrassing chars.  */
  1205.   while (count-- > 0) {
  1206.     *bp++ = '\\';
  1207.     *bp++ = '\n';
  1208.   }
  1209. }
  1210.  
  1211. /* Like newline_fix but for use within a directive-name.
  1212.    Move any backslash-newlines up past any following symbol constituents.  */
  1213.  
  1214. name_newline_fix (bp)
  1215.      U_CHAR *bp;
  1216. {
  1217.   register U_CHAR *p = bp;
  1218.   register int count = 0;
  1219.  
  1220.   /* First count the backslash-newline pairs here.  */
  1221.  
  1222.   while (*p++ == '\\' && *p++ == '\n')
  1223.     count++;
  1224.  
  1225.   p = bp + count * 2;
  1226.  
  1227.   /* What follows the backslash-newlines is not embarrassing.  */
  1228.  
  1229.   if (count == 0 || !is_idchar[*p])
  1230.     return;
  1231.  
  1232.   /* Copy all potentially embarrassing characters
  1233.      that follow the backslash-newline pairs
  1234.      down to where the pairs originally started.  */
  1235.  
  1236.   while (is_idchar[*p])
  1237.     *bp++ = *p++;
  1238.  
  1239.   /* Now write the same number of pairs after the embarrassing chars.  */
  1240.   while (count-- > 0) {
  1241.     *bp++ = '\\';
  1242.     *bp++ = '\n';
  1243.   }
  1244. }
  1245.  
  1246. /*
  1247.  * The main loop of the program.
  1248.  *
  1249.  * Read characters from the input stack, transferring them to the
  1250.  * output buffer OP.
  1251.  *
  1252.  * Macros are expanded and push levels on the input stack.
  1253.  * At the end of such a level it is popped off and we keep reading.
  1254.  * At the end of any other kind of level, we return.
  1255.  * #-directives are handled, except within macros.
  1256.  *
  1257.  * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
  1258.  * and insert them when appropriate.  This is set while scanning macro
  1259.  * arguments before substitution.  It is zero when scanning for final output.
  1260.  *   There are three types of Newline markers:
  1261.  *   * Newline -  follows a macro name that was not expanded
  1262.  *     because it appeared inside an expansion of the same macro.
  1263.  *     This marker prevents future expansion of that identifier.
  1264.  *     When the input is rescanned into the final output, these are deleted.
  1265.  *     These are also deleted by ## concatenation.
  1266.  *   * Newline Space (or Newline and any other whitespace character)
  1267.  *     stands for a place that tokens must be separated or whitespace
  1268.  *     is otherwise desirable, but where the ANSI standard specifies there
  1269.  *     is no whitespace.  This marker turns into a Space (or whichever other
  1270.  *     whitespace char appears in the marker) in the final output,
  1271.  *     but it turns into nothing in an argument that is stringified with #.
  1272.  *     Such stringified arguments are the only place where the ANSI standard
  1273.  *     specifies with precision that whitespace may not appear.
  1274.  *
  1275.  * During this function, IP->bufp is kept cached in IBP for speed of access.
  1276.  * Likewise, OP->bufp is kept in OBP.  Before calling a subroutine
  1277.  * IBP, IP and OBP must be copied back to memory.  IP and IBP are
  1278.  * copied back with the RECACHE macro.  OBP must be copied back from OP->bufp
  1279.  * explicitly, and before RECACHE, since RECACHE uses OBP.
  1280.  */
  1281.  
  1282. rescan (op, output_marks)
  1283.      FILE_BUF *op;
  1284.      int output_marks;
  1285. {
  1286.   /* Character being scanned in main loop.  */
  1287.   register U_CHAR c;
  1288.  
  1289.   /* Length of pending accumulated identifier.  */
  1290.   register int ident_length = 0;
  1291.  
  1292.   /* Hash code of pending accumulated identifier.  */
  1293.   register int hash = 0;
  1294.  
  1295.   /* Current input level (&instack[indepth]).  */
  1296.   FILE_BUF *ip;
  1297.  
  1298.   /* Pointer for scanning input.  */
  1299.   register U_CHAR *ibp;
  1300.  
  1301.   /* Pointer to end of input.  End of scan is controlled by LIMIT.  */
  1302.   register U_CHAR *limit;
  1303.  
  1304.   /* Pointer for storing output.  */
  1305.   register U_CHAR *obp;
  1306.  
  1307.   /* REDO_CHAR is nonzero if we are processing an identifier
  1308.      after backing up over the terminating character.
  1309.      Sometimes we process an identifier without backing up over
  1310.      the terminating character, if the terminating character
  1311.      is not special.  Backing up is done so that the terminating character
  1312.      will be dispatched on again once the identifier is dealt with.  */
  1313.   int redo_char = 0;
  1314.  
  1315.   /* 1 if within an identifier inside of which a concatenation
  1316.      marker (Newline -) has been seen.  */
  1317.   int concatenated = 0;
  1318.  
  1319.   /* While scanning a comment or a string constant,
  1320.      this records the line it started on, for error messages.  */
  1321.   int start_line;
  1322.  
  1323.   /* Record position of last `real' newline.  */
  1324.   U_CHAR *beg_of_line;
  1325.  
  1326. /* Pop the innermost input stack level, assuming it is a macro expansion.  */
  1327.  
  1328. #define POPMACRO \
  1329. do { ip->macro->type = T_MACRO;        \
  1330.      if (ip->free_ptr) free (ip->free_ptr);    \
  1331.      --indepth; } while (0)
  1332.  
  1333. /* Reload `rescan's local variables that describe the current
  1334.    level of the input stack.  */
  1335.  
  1336. #define RECACHE  \
  1337. do { ip = &instack[indepth];        \
  1338.      ibp = ip->bufp;            \
  1339.      limit = ip->buf + ip->length;    \
  1340.      op->bufp = obp;            \
  1341.      check_expand (op, limit - ibp);    \
  1342.      beg_of_line = 0;            \
  1343.      obp = op->bufp; } while (0)
  1344.  
  1345.   if (no_output && instack[indepth].fname != 0)
  1346.     skip_if_group (&instack[indepth], 1);
  1347.  
  1348.   obp = op->bufp;
  1349.   RECACHE;
  1350.   beg_of_line = ibp;
  1351.  
  1352.   /* Our caller must always put a null after the end of
  1353.      the input at each input stack level.  */
  1354.   if (*limit != 0)
  1355.     abort ();
  1356.  
  1357.   while (1) {
  1358.     c = *ibp++;
  1359.     *obp++ = c;
  1360.  
  1361.     switch (c) {
  1362.     case '\\':
  1363.       if (ibp >= limit)
  1364.     break;
  1365.       if (*ibp == '\n') {
  1366.     /* Always merge lines ending with backslash-newline,
  1367.        even in middle of identifier.  */
  1368.     ++ibp;
  1369.     ++ip->lineno;
  1370.     --obp;        /* remove backslash from obuf */
  1371.     break;
  1372.       }
  1373.       /* Otherwise, backslash suppresses specialness of following char,
  1374.      so copy it here to prevent the switch from seeing it.
  1375.      But first get any pending identifier processed.  */
  1376.       if (ident_length > 0)
  1377.     goto specialchar;
  1378.       *obp++ = *ibp++;
  1379.       break;
  1380.  
  1381.     case '#':
  1382.       /* If this is expanding a macro definition, don't recognize
  1383.      preprocessor directives.  */
  1384.       if (ip->macro != 0)
  1385.     goto randomchar;
  1386.       if (ident_length)
  1387.     goto specialchar;
  1388.  
  1389.       /* # keyword: a # must be first nonblank char on the line */
  1390.       if (beg_of_line == 0)
  1391.     goto randomchar;
  1392.       {
  1393.     U_CHAR *bp;
  1394.  
  1395.     /* Scan from start of line, skipping whitespace, comments
  1396.        and backslash-newlines, and see if we reach this #.
  1397.        If not, this # is not special.  */
  1398.     bp = beg_of_line;
  1399.     while (1) {
  1400.       if (is_hor_space[*bp])
  1401.         bp++;
  1402.       else if (*bp == '\\' && bp[1] == '\n')
  1403.         bp += 2;
  1404.       else if (*bp == '/' && (newline_fix (bp + 1), bp[1]) == '*') {
  1405.         bp += 2;
  1406.         while (!(*bp == '*' && (newline_fix (bp + 1), bp[1]) == '/'))
  1407.           bp++;
  1408.         bp += 1;
  1409.       }
  1410.       else if (cplusplus && *bp == '/' && (newline_fix (bp + 1), bp[1]) == '/') {
  1411.         bp += 2;
  1412.         while (*bp++ != '\n') ;
  1413.       }
  1414.       else break;
  1415.     }
  1416.     if (bp + 1 != ibp)
  1417.       goto randomchar;
  1418.       }
  1419.  
  1420.       /* This # can start a directive.  */
  1421.  
  1422.       --obp;        /* Don't copy the '#' */
  1423.  
  1424.       ip->bufp = ibp;
  1425.       op->bufp = obp;
  1426.       if (! handle_directive (ip, op)) {
  1427. #ifdef USE_C_ALLOCA
  1428.     alloca (0);
  1429. #endif
  1430.     /* Not a known directive: treat it as ordinary text.
  1431.        IP, OP, IBP, etc. have not been changed.  */
  1432.     if (no_output && instack[indepth].fname) {
  1433.       /* If not generating expanded output,
  1434.          what we do with ordinary text is skip it.
  1435.          Discard everything until next # directive.  */
  1436.       skip_if_group (&instack[indepth], 1);
  1437.       RECACHE;
  1438.       beg_of_line = ibp;
  1439.       break;
  1440.     }
  1441.     ++obp;        /* Copy the '#' after all */
  1442.     goto randomchar;
  1443.       }
  1444. #ifdef USE_C_ALLOCA
  1445.       alloca (0);
  1446. #endif
  1447.       /* A # directive has been successfully processed.  */
  1448.       /* If not generating expanded output, ignore everything until
  1449.      next # directive.  */
  1450.       if (no_output && instack[indepth].fname)
  1451.     skip_if_group (&instack[indepth], 1);
  1452.       obp = op->bufp;
  1453.       RECACHE;
  1454.       beg_of_line = ibp;
  1455.       break;
  1456.  
  1457.     case '\"':            /* skip quoted string */
  1458.     case '\'':
  1459.       /* A single quoted string is treated like a double -- some
  1460.      programs (e.g., troff) are perverse this way */
  1461.  
  1462.       if (ident_length)
  1463.     goto specialchar;
  1464.  
  1465.       start_line = ip->lineno;
  1466.  
  1467.       /* Skip ahead to a matching quote.  */
  1468.  
  1469.       while (1) {
  1470.     if (ibp >= limit) {
  1471.       if (traditional) {
  1472.         if (ip->macro != 0) {
  1473.           /* try harder: this string crosses a macro expansion boundary */
  1474.           POPMACRO;
  1475.           RECACHE;
  1476.           continue;
  1477.         }
  1478.       } else
  1479.         error_with_line (line_for_error (start_line),
  1480.                  "unterminated string or character constant");
  1481.       break;
  1482.     }
  1483.     *obp++ = *ibp;
  1484.     switch (*ibp++) {
  1485.     case '\n':
  1486.       ++ip->lineno;
  1487.       ++op->lineno;
  1488.       /* Traditionally, end of line ends a string constant with no error.
  1489.          So exit the loop and record the new line.  */
  1490.       if (traditional) {
  1491.         beg_of_line = ibp;
  1492.         goto while2end;
  1493.       }
  1494.       if (pedantic || c == '\'') {
  1495.         error_with_line (line_for_error (start_line),
  1496.                  "unterminated string or character constant");
  1497.         goto while2end;
  1498.       }
  1499.       break;
  1500.  
  1501.     case '\\':
  1502.       if (ibp >= limit)
  1503.         break;
  1504.       if (*ibp == '\n') {
  1505.         /* Backslash newline is replaced by nothing at all,
  1506.            but keep the line counts correct.  */
  1507.         --obp;
  1508.         ++ibp;
  1509.         ++ip->lineno;
  1510.       } else {
  1511.         /* ANSI stupidly requires that in \\ the second \
  1512.            is *not* prevented from combining with a newline.  */
  1513.         while (*ibp == '\\' && ibp[1] == '\n') {
  1514.           ibp += 2;
  1515.           ++ip->lineno;
  1516.         }
  1517.         *obp++ = *ibp++;
  1518.       }
  1519.       break;
  1520.  
  1521.     case '\"':
  1522.     case '\'':
  1523.       if (ibp[-1] == c)
  1524.         goto while2end;
  1525.       break;
  1526.     }
  1527.       }
  1528.     while2end:
  1529.       break;
  1530.  
  1531.     case '/':
  1532.       if (*ibp == '\\' && ibp[1] == '\n')
  1533.     newline_fix (ibp);
  1534.       /* Don't look for comments inside a macro definition.  */
  1535.       if (ip->macro != 0)
  1536.     goto randomchar;
  1537.       /* A comment constitutes white space, so it can terminate an identifier.
  1538.      Process the identifier, if any.  */
  1539.       if (ident_length)
  1540.     goto specialchar;
  1541.       if (cplusplus && *ibp == '/') {
  1542.     /* C++ style comment... */
  1543.     start_line = ip->lineno;
  1544.  
  1545.     --ibp;            /* Back over the slash */
  1546.     --obp;
  1547.  
  1548.     /* Comments are equivalent to spaces. */
  1549.     if (! put_out_comments)
  1550.       *obp++ = ' ';
  1551.     else {
  1552.       /* must fake up a comment here */
  1553.       *obp++ = '/';
  1554.       *obp++ = '/';
  1555.     }
  1556.     {
  1557.       U_CHAR *before_bp = ibp+2;
  1558.  
  1559.       while (ibp < limit) {
  1560.         if (*ibp == '\\' && ibp[1] == '\n') {
  1561.           ip->lineno++;
  1562.           ibp += 2;
  1563.         } else if (*ibp++ == '\n') {
  1564.           ibp--;
  1565.           if (put_out_comments) {
  1566.         bcopy (before_bp, obp, ibp - before_bp);
  1567.         obp += ibp - before_bp;
  1568.           }
  1569.           break;
  1570.         }
  1571.       }
  1572.       break;
  1573.     }
  1574.       }
  1575.       if (*ibp != '*')
  1576.     goto randomchar;
  1577.  
  1578.       /* We have a comment.  Skip it, optionally copying it to output.  */
  1579.  
  1580.       start_line = ip->lineno;
  1581.  
  1582.       ++ibp;            /* Skip the star. */
  1583.  
  1584.       /* Comments are equivalent to spaces.
  1585.      Note that we already output the slash; we might not want it.
  1586.      For -traditional, a comment is equivalent to nothing.  */
  1587.       if (! put_out_comments) {
  1588.     if (traditional)
  1589.       obp--;
  1590.     else
  1591.       obp[-1] = ' ';
  1592.       }
  1593.       else
  1594.     *obp++ = '*';
  1595.  
  1596.       {
  1597.     U_CHAR *before_bp = ibp;
  1598.  
  1599.     while (ibp < limit) {
  1600.       switch (*ibp++) {
  1601.       case '/':
  1602.         if (warn_comments && ibp < limit && *ibp == '*')
  1603.           warning("`/*' within comment");
  1604.         break;
  1605.       case '*':
  1606.         if (*ibp == '\\' && ibp[1] == '\n')
  1607.           newline_fix (ibp);
  1608.         if (ibp >= limit || *ibp == '/')
  1609.           goto comment_end;
  1610.         break;
  1611.       case '\n':
  1612.         ++ip->lineno;
  1613.         /* Copy the newline into the output buffer, in order to
  1614.            avoid the pain of a #line every time a multiline comment
  1615.            is seen.  */
  1616.         if (!put_out_comments)
  1617.           *obp++ = '\n';
  1618.         ++op->lineno;
  1619.       }
  1620.     }
  1621.       comment_end:
  1622.  
  1623.     if (ibp >= limit)
  1624.       error_with_line (line_for_error (start_line),
  1625.                "unterminated comment");
  1626.     else {
  1627.       ibp++;
  1628.       if (put_out_comments) {
  1629.         bcopy (before_bp, obp, ibp - before_bp);
  1630.         obp += ibp - before_bp;
  1631.       }
  1632.     }
  1633.       }
  1634.       break;
  1635.  
  1636.     case '$':
  1637.       if (!dollars_in_ident)
  1638.     goto randomchar;
  1639.       goto letter;
  1640.  
  1641.     case '0': case '1': case '2': case '3': case '4':
  1642.     case '5': case '6': case '7': case '8': case '9':
  1643.       /* If digit is not part of identifier, it starts a number,
  1644.      which means that following letters are not an identifier.
  1645.      "0x5" does not refer to an identifier "x5".
  1646.      So copy all alphanumerics that follow without accumulating
  1647.      as an identifier.  Periods also, for sake of "3.e7".  */
  1648.  
  1649.       if (ident_length == 0) {
  1650.     while (ibp < limit) {
  1651.       while (ibp < limit && ibp[0] == '\\' && ibp[1] == '\n') {
  1652.         ++ip->lineno;
  1653.         ibp += 2;
  1654.       }
  1655.       c = *ibp++;
  1656.       if (!isalnum (c) && c != '.' && c != '_') {
  1657.         --ibp;
  1658.         break;
  1659.       }
  1660.       *obp++ = c;
  1661.       /* A sign can be part of a preprocessing number
  1662.          if it follows an e.  */
  1663.       if (c == 'e' || c == 'E') {
  1664.         while (ibp < limit && ibp[0] == '\\' && ibp[1] == '\n') {
  1665.           ++ip->lineno;
  1666.           ibp += 2;
  1667.         }
  1668.         if (ibp < limit && (*ibp == '+' || *ibp == '-')) {
  1669.           *obp++ = *ibp++;
  1670.           /* But traditional C does not let the token go past the sign.  */
  1671.           if (traditional)
  1672.         break;
  1673.         }
  1674.       }
  1675.     }
  1676.     break;
  1677.       }
  1678.       /* fall through */
  1679.  
  1680.     case '_':
  1681.     case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  1682.     case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
  1683.     case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
  1684.     case 's': case 't': case 'u': case 'v': case 'w': case 'x':
  1685.     case 'y': case 'z':
  1686.     case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  1687.     case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
  1688.     case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
  1689.     case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
  1690.     case 'Y': case 'Z':
  1691.     letter:
  1692.       ident_length++;
  1693.       /* Compute step of hash function, to avoid a proc call on every token */
  1694.       hash = HASHSTEP (hash, c);
  1695.       break;
  1696.  
  1697.     case '\n':
  1698.       /* If reprocessing a macro expansion, newline is a special marker.  */
  1699.       if (ip->macro != 0) {
  1700.     /* Newline White is a "funny space" to separate tokens that are
  1701.        supposed to be separate but without space between.
  1702.        Here White means any horizontal whitespace character.
  1703.        Newline - marks a recursive macro use that is not
  1704.        supposed to be expandable.  */
  1705.  
  1706.     if (*ibp == '-') {
  1707.       /* Newline - inhibits expansion of preceding token.
  1708.          If expanding a macro arg, we keep the newline -.
  1709.          In final output, it is deleted.  */
  1710.       if (! concatenated) {
  1711.         ident_length = 0;
  1712.         hash = 0;
  1713.       }
  1714.       ibp++;
  1715.       if (!output_marks) {
  1716.         obp--;
  1717.       } else {
  1718.         /* If expanding a macro arg, keep the newline -.  */
  1719.         *obp++ = '-';
  1720.       }
  1721.     } else if (is_space[*ibp]) {
  1722.       /* Newline Space does not prevent expansion of preceding token
  1723.          so expand the preceding token and then come back.  */
  1724.       if (ident_length > 0)
  1725.         goto specialchar;
  1726.  
  1727.       /* If generating final output, newline space makes a space.  */
  1728.       if (!output_marks) {
  1729.         obp[-1] = *ibp++;
  1730.         /* And Newline Newline makes a newline, so count it.  */
  1731.         if (obp[-1] == '\n')
  1732.           op->lineno++;
  1733.       } else {
  1734.         /* If expanding a macro arg, keep the newline space.
  1735.            If the arg gets stringified, newline space makes nothing.  */
  1736.         *obp++ = *ibp++;
  1737.       }
  1738.     } else abort ();    /* Newline followed by something random?  */
  1739.     break;
  1740.       }
  1741.  
  1742.       /* If there is a pending identifier, handle it and come back here.  */
  1743.       if (ident_length > 0)
  1744.     goto specialchar;
  1745.  
  1746.       beg_of_line = ibp;
  1747.  
  1748.       /* Update the line counts and output a #line if necessary.  */
  1749.       ++ip->lineno;
  1750.       ++op->lineno;
  1751.       if (ip->lineno != op->lineno) {
  1752.     op->bufp = obp;
  1753.     output_line_command (ip, op, 1, same_file);
  1754.     check_expand (op, ip->length - (ip->bufp - ip->buf));
  1755.     obp = op->bufp;
  1756.       }
  1757.       break;
  1758.  
  1759.       /* Come here either after (1) a null character that is part of the input
  1760.      or (2) at the end of the input, because there is a null there.  */
  1761.     case 0:
  1762.       if (ibp <= limit)
  1763.     /* Our input really contains a null character.  */
  1764.     goto randomchar;
  1765.  
  1766.       /* At end of a macro-expansion level, pop it and read next level.  */
  1767.       if (ip->macro != 0) {
  1768.     obp--;
  1769.     ibp--;
  1770.     /* If traditional, and we have an identifier that ends here,
  1771.        process it now, so we get the right error for recursion.  */
  1772.     if (traditional && ident_length
  1773.         && ! is_idchar[*instack[indepth - 1].bufp]) {
  1774.       redo_char = 1;
  1775.       goto randomchar;
  1776.     }
  1777.     POPMACRO;
  1778.     RECACHE;
  1779.     break;
  1780.       }
  1781.  
  1782.       /* If we don't have a pending identifier,
  1783.      return at end of input.  */
  1784.       if (ident_length == 0) {
  1785.     obp--;
  1786.     ibp--;
  1787.     op->bufp = obp;
  1788.     ip->bufp = ibp;
  1789.     goto ending;
  1790.       }
  1791.  
  1792.       /* If we do have a pending identifier, just consider this null
  1793.      a special character and arrange to dispatch on it again.
  1794.      The second time, IDENT_LENGTH will be zero so we will return.  */
  1795.  
  1796.       /* Fall through */
  1797.  
  1798. specialchar:
  1799.  
  1800.       /* Handle the case of a character such as /, ', " or null
  1801.      seen following an identifier.  Back over it so that
  1802.      after the identifier is processed the special char
  1803.      will be dispatched on again.  */
  1804.  
  1805.       ibp--;
  1806.       obp--;
  1807.       redo_char = 1;
  1808.  
  1809.     default:
  1810.  
  1811. randomchar:
  1812.  
  1813.       if (ident_length > 0) {
  1814.     register HASHNODE *hp;
  1815.  
  1816.     /* We have just seen an identifier end.  If it's a macro, expand it.
  1817.  
  1818.        IDENT_LENGTH is the length of the identifier
  1819.        and HASH is its hash code.
  1820.  
  1821.        The identifier has already been copied to the output,
  1822.        so if it is a macro we must remove it.
  1823.  
  1824.        If REDO_CHAR is 0, the char that terminated the identifier
  1825.        has been skipped in the output and the input.
  1826.        OBP-IDENT_LENGTH-1 points to the identifier.
  1827.        If the identifier is a macro, we must back over the terminator.
  1828.  
  1829.        If REDO_CHAR is 1, the terminating char has already been
  1830.        backed over.  OBP-IDENT_LENGTH points to the identifier.  */
  1831.  
  1832.     for (hp = hashtab[MAKE_POS (hash) % HASHSIZE]; hp != NULL;
  1833.          hp = hp->next) {
  1834.  
  1835.       if (hp->length == ident_length) {
  1836.         U_CHAR *obufp_before_macroname;
  1837.         int op_lineno_before_macroname;
  1838.         register int i = ident_length;
  1839.         register U_CHAR *p = hp->name;
  1840.         register U_CHAR *q = obp - i;
  1841.         int disabled;
  1842.  
  1843.         if (! redo_char)
  1844.           q--;
  1845.  
  1846.         do {        /* All this to avoid a strncmp () */
  1847.           if (*p++ != *q++)
  1848.         goto hashcollision;
  1849.         } while (--i);
  1850.  
  1851.         /* We found a use of a macro name.
  1852.            see if the context shows it is a macro call.  */
  1853.  
  1854.         /* Back up over terminating character if not already done.  */
  1855.         if (! redo_char) {
  1856.           ibp--;
  1857.           obp--;
  1858.         }
  1859.  
  1860.         obufp_before_macroname = obp - ident_length;
  1861.         op_lineno_before_macroname = op->lineno;
  1862.  
  1863.         /* Record whether the macro is disabled.  */
  1864.         disabled = hp->type == T_DISABLED;
  1865.  
  1866.         /* This looks like a macro ref, but if the macro was disabled,
  1867.            just copy its name and put in a marker if requested.  */
  1868.  
  1869.         if (disabled) {
  1870. #if 0
  1871.           /* This error check caught useful cases such as
  1872.          #define foo(x,y) bar(x(y,0), y)
  1873.          foo(foo, baz)  */
  1874.           if (traditional)
  1875.         error ("recursive use of macro `%s'", hp->name);
  1876. #endif
  1877.  
  1878.           if (output_marks) {
  1879.         check_expand (op, limit - ibp + 2);
  1880.         *obp++ = '\n';
  1881.         *obp++ = '-';
  1882.           }
  1883.           break;
  1884.         }
  1885.  
  1886.         /* If macro wants an arglist, verify that a '(' follows.
  1887.            first skip all whitespace, copying it to the output
  1888.            after the macro name.  Then, if there is no '(',
  1889.            decide this is not a macro call and leave things that way.  */
  1890.         if ((hp->type == T_MACRO || hp->type == T_DISABLED)
  1891.         && hp->value.defn->nargs >= 0)
  1892.           {
  1893.         while (1) {
  1894.           /* Scan forward over whitespace, copying it to the output.  */
  1895.           if (ibp == limit && ip->macro != 0) {
  1896.             POPMACRO;
  1897.             RECACHE;
  1898.           }
  1899.           /* A comment: copy it unchanged or discard it.  */
  1900.           else if (*ibp == '/' && ibp+1 != limit && ibp[1] == '*') {
  1901.             if (put_out_comments) {
  1902.               *obp++ = '/';
  1903.               *obp++ = '*';
  1904.             } else if (! traditional) {
  1905.               *obp++ = ' ';
  1906.             }
  1907.             ibp += 2;
  1908.             while (ibp + 1 != limit
  1909.                && !(ibp[0] == '*' && ibp[1] == '/')) {
  1910.               /* We need not worry about newline-marks,
  1911.              since they are never found in comments.  */
  1912.               if (*ibp == '\n') {
  1913.             /* Newline in a file.  Count it.  */
  1914.             ++ip->lineno;
  1915.             ++op->lineno;
  1916.               }
  1917.               if (put_out_comments)
  1918.             *obp++ = *ibp++;
  1919.               else
  1920.             ibp++;
  1921.             }
  1922.             ibp += 2;
  1923.             if (put_out_comments) {
  1924.               *obp++ = '*';
  1925.               *obp++ = '/';
  1926.             }
  1927.           }
  1928.           else if (is_space[*ibp]) {
  1929.             *obp++ = *ibp++;
  1930.             if (ibp[-1] == '\n') {
  1931.               if (ip->macro == 0) {
  1932.             /* Newline in a file.  Count it.  */
  1933.             ++ip->lineno;
  1934.             ++op->lineno;
  1935.               } else if (!output_marks) {
  1936.             /* A newline mark, and we don't want marks
  1937.                in the output.  If it is newline-hyphen,
  1938.                discard it entirely.  Otherwise, it is
  1939.                newline-whitechar, so keep the whitechar.  */
  1940.             obp--;
  1941.             if (*ibp == '-')
  1942.               ibp++;
  1943.             else {
  1944.               if (*ibp == '\n')
  1945.                 ++op->lineno;
  1946.               *obp++ = *ibp++;
  1947.             }
  1948.               } else {
  1949.             /* A newline mark; copy both chars to the output.  */
  1950.             *obp++ = *ibp++;
  1951.               }
  1952.             }
  1953.           }
  1954.           else break;
  1955.         }
  1956.         if (*ibp != '(')
  1957.           break;
  1958.           }
  1959.  
  1960.         /* This is now known to be a macro call.
  1961.            Discard the macro name from the output,
  1962.            along with any following whitespace just copied.  */
  1963.         obp = obufp_before_macroname;
  1964.         op->lineno = op_lineno_before_macroname;
  1965.  
  1966.         /* Expand the macro, reading arguments as needed,
  1967.            and push the expansion on the input stack.  */
  1968.         ip->bufp = ibp;
  1969.         op->bufp = obp;
  1970.         macroexpand (hp, op);
  1971.  
  1972.         /* Reexamine input stack, since macroexpand has pushed
  1973.            a new level on it.  */
  1974.         obp = op->bufp;
  1975.         RECACHE;
  1976.         break;
  1977.       }
  1978. hashcollision:
  1979.            ;
  1980.     }            /* End hash-table-search loop */
  1981.     ident_length = hash = 0; /* Stop collecting identifier */
  1982.     redo_char = 0;
  1983.     concatenated = 0;
  1984.       }                /* End if (ident_length > 0) */
  1985.     }                /* End switch */
  1986.   }                /* End per-char loop */
  1987.  
  1988.   /* Come here to return -- but first give an error message
  1989.      if there was an unterminated successful conditional.  */
  1990.  ending:
  1991.   if (if_stack != ip->if_stack) {
  1992.     char *str;
  1993.     switch (if_stack->type) {
  1994.     case T_IF:
  1995.       str = "if";
  1996.       break;
  1997.     case T_IFDEF:
  1998.       str = "ifdef";
  1999.       break;
  2000.     case T_IFNDEF:
  2001.       str = "ifndef";
  2002.       break;
  2003.     case T_ELSE:
  2004.       str = "else";
  2005.       break;
  2006.     case T_ELIF:
  2007.       str = "elif";
  2008.       break;
  2009.     }
  2010.     error_with_line (line_for_error (if_stack->lineno),
  2011.              "unterminated #%s conditional", str);
  2012.   }
  2013.   if_stack = ip->if_stack;
  2014. }
  2015.  
  2016. /*
  2017.  * Rescan a string into a temporary buffer and return the result
  2018.  * as a FILE_BUF.  Note this function returns a struct, not a pointer.
  2019.  *
  2020.  * OUTPUT_MARKS nonzero means keep Newline markers found in the input
  2021.  * and insert such markers when appropriate.  See `rescan' for details.
  2022.  * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
  2023.  * before substitution; it is 0 for other uses.
  2024.  */
  2025. FILE_BUF
  2026. expand_to_temp_buffer (buf, limit, output_marks)
  2027.      U_CHAR *buf, *limit;
  2028.      int output_marks;
  2029. {
  2030.   register FILE_BUF *ip;
  2031.   FILE_BUF obuf;
  2032.   int length = limit - buf;
  2033.   U_CHAR *buf1;
  2034.   int odepth = indepth;
  2035.  
  2036.   if (length < 0)
  2037.     abort ();
  2038.  
  2039.   /* Set up the input on the input stack.  */
  2040.  
  2041.   buf1 = (U_CHAR *) alloca (length + 1);
  2042.   {
  2043.     register U_CHAR *p1 = buf;
  2044.     register U_CHAR *p2 = buf1;
  2045.  
  2046.     while (p1 != limit)
  2047.       *p2++ = *p1++;
  2048.   }
  2049.   buf1[length] = 0;
  2050.  
  2051.   /* Set up to receive the output.  */
  2052.  
  2053.   obuf.length = length * 2 + 100; /* Usually enough.  Why be stingy?  */
  2054.   obuf.bufp = obuf.buf = (U_CHAR *) xmalloc (obuf.length);
  2055.   obuf.fname = 0;
  2056.   obuf.macro = 0;
  2057.   obuf.free_ptr = 0;
  2058.  
  2059.   CHECK_DEPTH ({return obuf;});
  2060.  
  2061.   ++indepth;
  2062.  
  2063.   ip = &instack[indepth];
  2064.   ip->fname = 0;
  2065.   ip->macro = 0;
  2066.   ip->free_ptr = 0;
  2067.   ip->length = length;
  2068.   ip->buf = ip->bufp = buf1;
  2069.   ip->if_stack = if_stack;
  2070.  
  2071.   ip->lineno = obuf.lineno = 1;
  2072.  
  2073.   /* Scan the input, create the output.  */
  2074.  
  2075.   rescan (&obuf, output_marks);
  2076.  
  2077.   /* Pop input stack to original state.  */
  2078.   --indepth;
  2079.  
  2080.   if (indepth != odepth)
  2081.     abort ();
  2082.  
  2083.   /* Record the output.  */
  2084.   obuf.length = obuf.bufp - obuf.buf;
  2085.  
  2086.   return obuf;
  2087. }
  2088.  
  2089. /*
  2090.  * Process a # directive.  Expects IP->bufp to point to the '#', as in
  2091.  * `#define foo bar'.  Passes to the command handler
  2092.  * (do_define, do_include, etc.): the addresses of the 1st and
  2093.  * last chars of the command (starting immediately after the #
  2094.  * keyword), plus op and the keyword table pointer.  If the command
  2095.  * contains comments it is copied into a temporary buffer sans comments
  2096.  * and the temporary buffer is passed to the command handler instead.
  2097.  * Likewise for backslash-newlines.
  2098.  *
  2099.  * Returns nonzero if this was a known # directive.
  2100.  * Otherwise, returns zero, without advancing the input pointer.
  2101.  */
  2102.  
  2103. int
  2104. handle_directive (ip, op)
  2105.      FILE_BUF *ip, *op;
  2106. {
  2107.   register U_CHAR *bp, *cp;
  2108.   register struct directive *kt;
  2109.   register int ident_length;
  2110.   U_CHAR *resume_p;
  2111.  
  2112.   /* Nonzero means we must copy the entire command
  2113.      to get rid of comments or backslash-newlines.  */
  2114.   int copy_command = 0;
  2115.  
  2116.   U_CHAR *ident, *after_ident;
  2117.  
  2118.   bp = ip->bufp;
  2119.   /* Skip whitespace and \-newline.  */
  2120.   while (1) {
  2121.     if (is_hor_space[*bp])
  2122.       bp++;
  2123.     else if (*bp == '/' && (newline_fix (bp + 1), bp[1]) == '*') {
  2124.       ip->bufp = bp;
  2125.       skip_to_end_of_comment (ip, &ip->lineno);
  2126.       bp = ip->bufp;
  2127.     } else if (*bp == '\\' && bp[1] == '\n') {
  2128.       bp += 2; ip->lineno++;
  2129.     } else break;
  2130.   }
  2131.  
  2132.   /* Now find end of directive name.
  2133.      If we encounter a backslash-newline, exchange it with any following
  2134.      symbol-constituents so that we end up with a contiguous name.  */
  2135.  
  2136.   cp = bp;
  2137.   while (1) {
  2138.     if (is_idchar[*cp])
  2139.       cp++;
  2140.     else {
  2141.       if (*cp == '\\' && cp[1] == '\n')
  2142.     name_newline_fix (cp);
  2143.       if (is_idchar[*cp])
  2144.     cp++;
  2145.       else break;
  2146.     }
  2147.   }
  2148.   ident_length = cp - bp;
  2149.   ident = bp;
  2150.   after_ident = cp;
  2151.  
  2152.   /* A line of just `#' becomes blank.  */
  2153.  
  2154.   if (ident_length == 0 && *after_ident == '\n') {
  2155.     ip->bufp = after_ident;
  2156.     return 1;
  2157.   }
  2158.  
  2159.   /*
  2160.    * Decode the keyword and call the appropriate expansion
  2161.    * routine, after moving the input pointer up to the next line.
  2162.    */
  2163.   for (kt = directive_table; kt->length > 0; kt++) {
  2164.     if (kt->length == ident_length && !strncmp (kt->name, ident, ident_length)) {
  2165.       register U_CHAR *buf;
  2166.       register U_CHAR *limit = ip->buf + ip->length;
  2167.       int unterminated = 0;
  2168.  
  2169.       /* Nonzero means do not delete comments within the directive.
  2170.      #define needs this when -traditional.  */
  2171.       int keep_comments = traditional && kt->traditional_comments;
  2172.  
  2173.       /* Find the end of this command (first newline not backslashed
  2174.      and not in a string or comment).
  2175.      Set COPY_COMMAND if the command must be copied
  2176.      (it contains a backslash-newline or a comment).  */
  2177.  
  2178.       buf = bp = after_ident;
  2179.       while (bp < limit) {
  2180.     register U_CHAR c = *bp++;
  2181.     switch (c) {
  2182.     case '\\':
  2183.       if (bp < limit) {
  2184.         if (*bp == '\n') {
  2185.           ip->lineno++;
  2186.           copy_command = 1;
  2187.         }
  2188.         bp++;
  2189.       }
  2190.       break;
  2191.  
  2192.     case '\'':
  2193.     case '\"':
  2194.       bp = skip_quoted_string (bp - 1, limit, ip->lineno, &ip->lineno, ©_command, &unterminated);
  2195.       /* Don't bother calling the directive if we already got an error
  2196.          message due to unterminated string.  Skip everything and pretend
  2197.          we called the directive.  */
  2198.       if (unterminated) {
  2199.         if (traditional) {
  2200.           /* Traditional preprocessing permits unterminated strings.  */
  2201.           ip->bufp = bp;
  2202.           goto endloop1;
  2203.         }
  2204.         ip->bufp = bp;
  2205.         return 1;
  2206.       }
  2207.       break;
  2208.  
  2209.       /* <...> is special for #include.  */
  2210.     case '<':
  2211.       if (!kt->angle_brackets)
  2212.         break;
  2213.       while (*bp && *bp != '>') bp++;
  2214.       break;
  2215.  
  2216.     case '/':
  2217.       if (*bp == '\\' && bp[1] == '\n')
  2218.         newline_fix (bp);
  2219.       if (*bp == '*'
  2220.           || (cplusplus && *bp == '/')) {
  2221.         U_CHAR *obp = bp - 1;
  2222.         ip->bufp = bp + 1;
  2223.         skip_to_end_of_comment (ip, &ip->lineno);
  2224.         bp = ip->bufp;
  2225.         /* No need to copy the command because of a comment at the end;
  2226.            just don't include the comment in the directive.  */
  2227.         if (bp == limit || *bp == '\n') {
  2228.           bp = obp;
  2229.           goto endloop1;
  2230.         }
  2231.         /* Don't remove the comments if -traditional.  */
  2232.         if (! keep_comments)
  2233.           copy_command++;
  2234.       }
  2235.       break;
  2236.  
  2237.     case '\n':
  2238.       --bp;        /* Point to the newline */
  2239.       ip->bufp = bp;
  2240.       goto endloop1;
  2241.     }
  2242.       }
  2243.       ip->bufp = bp;
  2244.  
  2245.     endloop1:
  2246.       resume_p = ip->bufp;
  2247.       /* BP is the end of the directive.
  2248.      RESUME_P is the next interesting data after the directive.
  2249.      A comment may come between.  */
  2250.  
  2251.       if (copy_command) {
  2252.     register U_CHAR *xp = buf;
  2253.     /* Need to copy entire command into temp buffer before dispatching */
  2254.  
  2255.     cp = (U_CHAR *) alloca (bp - buf + 5); /* room for cmd plus
  2256.                           some slop */
  2257.     buf = cp;
  2258.  
  2259.     /* Copy to the new buffer, deleting comments
  2260.        and backslash-newlines (and whitespace surrounding the latter).  */
  2261.  
  2262.     while (xp < bp) {
  2263.       register U_CHAR c = *xp++;
  2264.       *cp++ = c;
  2265.  
  2266.       switch (c) {
  2267.       case '\n':
  2268.         break;
  2269.  
  2270.         /* <...> is special for #include.  */
  2271.       case '<':
  2272.         if (!kt->angle_brackets)
  2273.           break;
  2274.         while (xp < bp && c != '>') {
  2275.           c = *xp++;
  2276.           if (c == '\\' && xp < bp && *xp == '\n')
  2277.         xp++, ip->lineno++;
  2278.           else
  2279.         *cp++ = c;
  2280.         }
  2281.         break;
  2282.  
  2283.       case '\\':
  2284.         if (*xp == '\n') {
  2285.           xp++;
  2286.           cp--;
  2287.           if (cp != buf && is_space[cp[-1]]) {
  2288.         while (cp != buf && is_space[cp[-1]]) cp--;
  2289.         cp++;
  2290.         SKIP_WHITE_SPACE (xp);
  2291.           } else if (is_space[*xp]) {
  2292.         *cp++ = *xp++;
  2293.         SKIP_WHITE_SPACE (xp);
  2294.           }
  2295.         }
  2296.         break;
  2297.  
  2298.       case '\'':
  2299.       case '\"':
  2300.         {
  2301.           register U_CHAR *bp1
  2302.         = skip_quoted_string (xp - 1, limit, ip->lineno, 0, 0, 0);
  2303.           while (xp != bp1)
  2304.         *cp++ = *xp++;
  2305.         }
  2306.         break;
  2307.  
  2308.       case '/':
  2309.         if (*xp == '*'
  2310.         || (cplusplus && *xp == '/')) {
  2311.           ip->bufp = xp + 1;
  2312.           skip_to_end_of_comment (ip, 0);
  2313.           if (keep_comments)
  2314.         while (xp != ip->bufp)
  2315.           *cp++ = *xp++;
  2316.           /* Delete or replace the slash.  */
  2317.           else if (traditional)
  2318.         cp--;
  2319.           else
  2320.         cp[-1] = ' ';
  2321.           xp = ip->bufp;
  2322.         }
  2323.       }
  2324.     }
  2325.  
  2326.     /* Null-terminate the copy.  */
  2327.  
  2328.     *cp = 0;
  2329.       }
  2330.       else
  2331.     cp = bp;
  2332.  
  2333.       ip->bufp = resume_p;
  2334.  
  2335.       /* Some directives should be written out for cc1 to process,
  2336.      just as if they were not defined.  */
  2337.  
  2338.       if (kt->pass_thru) {
  2339.         int len;
  2340.  
  2341.     /* Output directive name.  */
  2342.         check_expand (op, kt->length+1);
  2343.         *op->bufp++ = '#';
  2344.         bcopy (kt->name, op->bufp, kt->length);
  2345.         op->bufp += kt->length;
  2346.  
  2347.     /* Output arguments.  */
  2348.         len = (cp - buf);
  2349.         check_expand (op, len);
  2350.         bcopy (buf, op->bufp, len);
  2351.         op->bufp += len;
  2352.       }
  2353.  
  2354.       /* Call the appropriate command handler.  buf now points to
  2355.      either the appropriate place in the input buffer, or to
  2356.      the temp buffer if it was necessary to make one.  cp
  2357.      points to the first char after the contents of the (possibly
  2358.      copied) command, in either case. */
  2359.       (*kt->func) (buf, cp, op, kt);
  2360.       check_expand (op, ip->length - (ip->bufp - ip->buf));
  2361.  
  2362.       return 1;
  2363.     }
  2364.   }
  2365.  
  2366.   return 0;
  2367. }
  2368.  
  2369. static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  2370.                  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
  2371.                 };
  2372.  
  2373. /*
  2374.  * expand things like __FILE__.  Place the expansion into the output
  2375.  * buffer *without* rescanning.
  2376.  */
  2377. special_symbol (hp, op)
  2378.      HASHNODE *hp;
  2379.      FILE_BUF *op;
  2380. {
  2381.   char *buf;
  2382.   time_t t;
  2383.   int i, len;
  2384.   int true_indepth;
  2385.   FILE_BUF *ip = NULL;
  2386.   static struct tm *timebuf = NULL;
  2387.   struct tm *localtime ();
  2388.  
  2389.   int paren = 0;        /* For special `defined' keyword */
  2390.  
  2391.   for (i = indepth; i >= 0; i--)
  2392.     if (instack[i].fname != NULL) {
  2393.       ip = &instack[i];
  2394.       break;
  2395.     }
  2396.   if (ip == NULL) {
  2397.     error ("cccp error: not in any file?!");
  2398.     return;            /* the show must go on */
  2399.   }
  2400.  
  2401.   switch (hp->type) {
  2402.   case T_FILE:
  2403.   case T_BASE_FILE:
  2404.     {
  2405.       char *string;
  2406.       if (hp->type == T_FILE)
  2407.     string = ip->fname;
  2408.       else
  2409.     string = instack[0].fname;
  2410.  
  2411.       if (string)
  2412.     {
  2413.       buf = (char *) alloca (3 + strlen (string));
  2414.       sprintf (buf, "\"%s\"", string);
  2415.     }
  2416.       else
  2417.     buf = "\"\"";
  2418.  
  2419.       break;
  2420.     }
  2421.  
  2422.   case T_INCLUDE_LEVEL:
  2423.     true_indepth = 0;
  2424.     for (i = indepth; i >= 0; i--)
  2425.       if (instack[i].fname != NULL)
  2426.         true_indepth++;
  2427.  
  2428.     buf = (char *) alloca (8);    /* Eigth bytes ought to be more than enough */
  2429.     sprintf (buf, "%d", true_indepth - 1);
  2430.     break;
  2431.  
  2432.   case T_VERSION:
  2433.     buf = (char *) alloca (3 + strlen (version_string));
  2434.     sprintf (buf, "\"%s\"", version_string);
  2435.     break;
  2436.  
  2437.   case T_CONST:
  2438.     buf = (char *) alloca (4 * sizeof (int));
  2439.     sprintf (buf, "%d", hp->value.ival);
  2440.     break;
  2441.  
  2442.   case T_SPECLINE:
  2443.     buf = (char *) alloca (10);
  2444.     sprintf (buf, "%d", ip->lineno);
  2445.     break;
  2446.  
  2447.   case T_DATE:
  2448.   case T_TIME:
  2449.     if (timebuf == NULL) {
  2450.       t = time (0);
  2451.       timebuf = localtime (&t);
  2452.     }
  2453.     buf = (char *) alloca (20);
  2454.     if (hp->type == T_DATE)
  2455.       sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
  2456.           timebuf->tm_mday, timebuf->tm_year + 1900);
  2457.     else
  2458.       sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
  2459.           timebuf->tm_sec);
  2460.     break;
  2461.  
  2462.   case T_SPEC_DEFINED:
  2463.     buf = " 0 ";        /* Assume symbol is not defined */
  2464.     ip = &instack[indepth];
  2465.     SKIP_WHITE_SPACE (ip->bufp);
  2466.     if (*ip->bufp == '(') {
  2467.       paren++;
  2468.       ip->bufp++;            /* Skip over the paren */
  2469.       SKIP_WHITE_SPACE (ip->bufp);
  2470.     }
  2471.  
  2472.     if (!is_idstart[*ip->bufp])
  2473.       goto oops;
  2474.     if (lookup (ip->bufp, -1, -1))
  2475.       buf = " 1 ";
  2476.     while (is_idchar[*ip->bufp])
  2477.       ++ip->bufp;
  2478.     SKIP_WHITE_SPACE (ip->bufp);
  2479.     if (paren) {
  2480.       if (*ip->bufp != ')')
  2481.     goto oops;
  2482.       ++ip->bufp;
  2483.     }
  2484.     break;
  2485.  
  2486. oops:
  2487.  
  2488.     error ("`defined' must be followed by ident or (ident)");
  2489.     break;
  2490.  
  2491.   default:
  2492.     error ("cccp error: invalid special hash type"); /* time for gdb */
  2493.     abort ();
  2494.   }
  2495.   len = strlen (buf);
  2496.   check_expand (op, len);
  2497.   bcopy (buf, op->bufp, len);
  2498.   op->bufp += len;
  2499.  
  2500.   return;
  2501. }
  2502.  
  2503.  
  2504. /* Routines to handle #directives */
  2505.  
  2506. /*
  2507.  * Process include file by reading it in and calling rescan.
  2508.  * Expects to see "fname" or <fname> on the input.
  2509.  */
  2510.  
  2511. do_include (buf, limit, op, keyword)
  2512.      U_CHAR *buf, *limit;
  2513.      FILE_BUF *op;
  2514.      struct directive *keyword;
  2515. {
  2516.   char *fname;        /* Dynamically allocated fname buffer */
  2517.   U_CHAR *fbeg, *fend;        /* Beginning and end of fname */
  2518.  
  2519.   struct file_name_list *stackp = include; /* Chain of dirs to search */
  2520.   struct file_name_list dsp[1];    /* First in chain, if #include "..." */
  2521.   int flen;
  2522.  
  2523.   int f;            /* file number */
  2524.  
  2525.   int retried = 0;        /* Have already tried macro
  2526.                    expanding the include line*/
  2527.   FILE_BUF trybuf;        /* It got expanded into here */
  2528.   int system_header_p = 0;    /* 0 for "...", 1 for <...> */
  2529.  
  2530.   f= -1;            /* JF we iz paranoid! */
  2531.  
  2532. get_filename:
  2533.  
  2534.   fbeg = buf;
  2535.   SKIP_WHITE_SPACE (fbeg);
  2536.   /* Discard trailing whitespace so we can easily see
  2537.      if we have parsed all the significant chars we were given.  */
  2538.   while (limit != fbeg && is_hor_space[limit[-1]]) limit--;
  2539.  
  2540.   switch (*fbeg++) {
  2541.   case '\"':
  2542.     fend = fbeg;
  2543.     while (fend != limit && *fend != '\"')
  2544.       fend++;
  2545.     if (*fend == '\"' && fend + 1 == limit) {
  2546.       FILE_BUF *fp;
  2547.  
  2548.       /* We have "filename".  Figure out directory this source
  2549.      file is coming from and put it on the front of the list. */
  2550.  
  2551.       /* If -I- was specified, don't search current dir, only spec'd ones. */
  2552.       if (ignore_srcdir) break;
  2553.  
  2554.       for (fp = &instack[indepth]; fp >= instack; fp--)
  2555.     {
  2556.       int n;
  2557.       char *ep,*nam;
  2558.       extern char *rindex ();
  2559.  
  2560.       if ((nam = fp->fname) != NULL) {
  2561.         /* Found a named file.  Figure out dir of the file,
  2562.            and put it in front of the search list.  */
  2563.         dsp[0].next = stackp;
  2564.         stackp = dsp;
  2565. #ifndef VMS
  2566.         ep = rindex (nam, '/');
  2567. #ifdef amigados
  2568.         if (!ep) 
  2569.           {
  2570.             ep = index (nam, ':');
  2571.             if (ep) ep++;
  2572.           }
  2573. #endif
  2574. #else                /* VMS */
  2575.         ep = rindex (nam, ']');
  2576.         if (ep == NULL) ep = rindex (nam, '>');
  2577.         if (ep == NULL) ep = rindex (nam, ':');
  2578.         if (ep != NULL) ep++;
  2579. #endif                /* VMS */
  2580.         if (ep != NULL) {
  2581.           n = ep - nam;
  2582.           dsp[0].fname = (char *) alloca (n + 1);
  2583.           strncpy (dsp[0].fname, nam, n);
  2584.           dsp[0].fname[n] = '\0';
  2585.           if (n > max_include_len) max_include_len = n;
  2586.         } else {
  2587.           dsp[0].fname = 0; /* Current directory */
  2588.         }
  2589.         break;
  2590.       }
  2591.     }
  2592.       break;
  2593.     }
  2594.     goto fail;
  2595.  
  2596.   case '<':
  2597.     fend = fbeg;
  2598.     while (fend != limit && *fend != '>') fend++;
  2599.     if (*fend == '>' && fend + 1 == limit) {
  2600.       system_header_p = 1;
  2601.       /* If -I-, start with the first -I dir after the -I-.  */
  2602.       if (first_bracket_include)
  2603.     stackp = first_bracket_include;
  2604.       break;
  2605.     }
  2606.     goto fail;
  2607.  
  2608.   default:
  2609.   fail:
  2610.     if (retried) {
  2611.       error ("#include expects \"fname\" or <fname>");
  2612.       return;
  2613.     } else {
  2614.       trybuf = expand_to_temp_buffer (buf, limit, 0);
  2615.       buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
  2616.       bcopy (trybuf.buf, buf, trybuf.bufp - trybuf.buf);
  2617.       limit = buf + (trybuf.bufp - trybuf.buf);
  2618.       free (trybuf.buf);
  2619.       retried++;
  2620.       goto get_filename;
  2621.     }
  2622.   }
  2623.  
  2624.   flen = fend - fbeg;
  2625.   fname = (char *) alloca (max_include_len + flen + 2);
  2626.   /* + 2 above for slash and terminating null.  */
  2627.  
  2628.   /* If specified file name is absolute, just open it.  */
  2629.  
  2630.   if (*fbeg == '/'
  2631. #ifdef amigados
  2632.            || nindex (fbeg, ':', flen)
  2633. #endif
  2634.                         ) {
  2635.     strncpy (fname, fbeg, flen);
  2636.     fname[flen] = 0;
  2637.     f = open (fname, O_RDONLY, 0666);
  2638.   } else {
  2639.     /* Search directory path, trying to open the file.
  2640.        Copy each filename tried into FNAME.  */
  2641.  
  2642.     for (; stackp; stackp = stackp->next) {
  2643.       if (stackp->fname) {
  2644.     strcpy (fname, stackp->fname);
  2645. #ifdef amigados
  2646.     if (fname[strlen (fname) - 1] != ':')
  2647. #endif
  2648.     strcat (fname, "/");
  2649.     fname[strlen (fname) + flen] = 0;
  2650.       } else {
  2651.     fname[0] = 0;
  2652.       }
  2653.       strncat (fname, fbeg, flen);
  2654. #ifdef VMS
  2655.       /* Change this 1/2 Unix 1/2 VMS file specification into a
  2656.          full VMS file specification */
  2657.       if (stackp->fname && (stackp->fname[0] != 0)) {
  2658.     /* Fix up the filename */
  2659.     hack_vms_include_specification (fname);
  2660.       } else {
  2661.           /* This is a normal VMS filespec, so use it unchanged.  */
  2662.     strncpy (fname, fbeg, flen);
  2663.     fname[flen] = 0;
  2664.       }
  2665. #endif /* VMS */
  2666.       if ((f = open (fname, O_RDONLY, 0666)) >= 0)
  2667.     break;
  2668.     }
  2669.   }
  2670.  
  2671.   if (f < 0) {
  2672.     strncpy (fname, fbeg, flen);
  2673.     fname[flen] = 0;
  2674.     error_from_errno (fname);
  2675.  
  2676.     /* For -M, add this file to the dependencies.  */
  2677.     if (print_deps > (system_header_p || (system_include_depth > 0))) {
  2678.       if (system_header_p)
  2679.     warning ("nonexistent file <%.*s> omitted from dependency output",
  2680.          fend - fbeg, fbeg);
  2681.       else
  2682.     {
  2683.       deps_output (fbeg, fend - fbeg);
  2684.       deps_output (" ", 0);
  2685.     }
  2686.     }
  2687.   } else {
  2688.  
  2689.     /* Check to see if this include file is a once-only include file.
  2690.        If so, give up.  */
  2691.  
  2692.     struct file_name_list* ptr;
  2693.  
  2694.     for (ptr = dont_repeat_files; ptr; ptr = ptr->next) {
  2695.       if (!strcmp (ptr->fname, fname)) {
  2696.     close (f);
  2697.         return;                /* This file was once'd. */
  2698.       }
  2699.     }
  2700.  
  2701.     for (ptr = all_include_files; ptr; ptr = ptr->next) {
  2702.       if (!strcmp (ptr->fname, fname))
  2703.         break;                /* This file was included before. */
  2704.     }
  2705.  
  2706.     if (ptr == 0) {
  2707.       /* This is the first time for this file.  */
  2708.       /* Add it to list of files included.  */
  2709.  
  2710.       ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  2711.       ptr->next = all_include_files;
  2712.       all_include_files = ptr;
  2713.       ptr->fname = savestring (fname);
  2714.  
  2715.       /* For -M, add this file to the dependencies.  */
  2716.       if (print_deps > (system_header_p || (system_include_depth > 0))) {
  2717.     deps_output (fname, strlen (fname));
  2718.     deps_output (" ", 0);
  2719.       }
  2720.     }   
  2721.  
  2722.     if (system_header_p)
  2723.       system_include_depth++;
  2724.  
  2725.     /* Actually process the file.  */
  2726.     finclude (f, fname, op);
  2727.  
  2728.     if (system_header_p)
  2729.       system_include_depth--;
  2730.  
  2731.     close (f);
  2732.   }
  2733. }
  2734.  
  2735. /* Process the contents of include file FNAME, already open on descriptor F,
  2736.    with output to OP.  */
  2737.  
  2738. finclude (f, fname, op)
  2739.      int f;
  2740.      char *fname;
  2741.      FILE_BUF *op;
  2742. {
  2743.   int st_mode;
  2744.   long st_size;
  2745.   long i;
  2746.   FILE_BUF *fp;            /* For input stack frame */
  2747.   int success = 0;
  2748.  
  2749.   CHECK_DEPTH (return;);
  2750.  
  2751.   if (file_size_and_mode (f, &st_mode, &st_size) < 0)
  2752.     goto nope;        /* Impossible? */
  2753.  
  2754.   fp = &instack[indepth + 1];
  2755.   bzero (fp, sizeof (FILE_BUF));
  2756.   fp->fname = fname;
  2757.   fp->length = 0;
  2758.   fp->lineno = 1;
  2759.   fp->if_stack = if_stack;
  2760.  
  2761.   if (S_ISREG (st_mode)) {
  2762.     fp->buf = (U_CHAR *) alloca (st_size + 2);
  2763.     fp->bufp = fp->buf;
  2764.  
  2765.     /* Read the file contents, knowing that st_size is an upper bound
  2766.        on the number of bytes we can read.  */
  2767.     while (st_size > 0) {
  2768.       i = read (f, fp->buf + fp->length, st_size);
  2769.       if (i <= 0) {
  2770.     if (i == 0) break;
  2771.     goto nope;
  2772.       }
  2773.       fp->length += i;
  2774.       st_size -= i;
  2775.     }
  2776.   }
  2777.   else {
  2778.     /* Cannot count its file size before reading.
  2779.        First read the entire file into heap and
  2780.        copy them into buffer on stack. */
  2781.  
  2782.     U_CHAR *bufp;
  2783.     U_CHAR *basep;
  2784.     int bsize = 2000;
  2785.  
  2786.     st_size = 0;
  2787.     basep = (U_CHAR *) xmalloc (bsize + 2);
  2788.     bufp = basep;
  2789.  
  2790.     for (;;) {
  2791.       i = read (f, bufp, bsize - st_size);
  2792.       if (i < 0)
  2793.     goto nope;      /* error! */
  2794.       if (i == 0)
  2795.     break;    /* End of file */
  2796.       st_size += i;
  2797.       bufp += i;
  2798.       if (bsize == st_size) {    /* Buffer is full! */
  2799.       bsize *= 2;
  2800.       basep = (U_CHAR *) xrealloc (basep, bsize + 2);
  2801.       bufp = basep + st_size;    /* May have moved */
  2802.     }
  2803.     }
  2804.     fp->buf = (U_CHAR *) alloca (st_size + 2);
  2805.     fp->bufp = fp->buf;
  2806.     bcopy (basep, fp->buf, st_size);
  2807.     fp->length = st_size;
  2808.     free (basep);
  2809.   }
  2810.  
  2811.   if (!no_trigraphs)
  2812.     trigraph_pcp (fp);
  2813.  
  2814.   if (fp->length > 0 && fp->buf[fp->length-1] != '\n')
  2815.     fp->buf[fp->length++] = '\n';
  2816.   fp->buf[fp->length] = '\0';
  2817.  
  2818.   success = 1;
  2819.   indepth++;
  2820.  
  2821.   output_line_command (fp, op, 0, enter_file);
  2822.   rescan (op, 0);
  2823.   indepth--;
  2824.   output_line_command (&instack[indepth], op, 0, leave_file);
  2825.  
  2826. nope:
  2827.  
  2828.   if (!success)
  2829.     perror_with_name (fname);
  2830.  
  2831.   close (f);
  2832. }
  2833.  
  2834. /* The arglist structure is built by do_define to tell
  2835.    collect_definition where the argument names begin.  That
  2836.    is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
  2837.    would contain pointers to the strings x, y, and z.
  2838.    Collect_definition would then build a DEFINITION node,
  2839.    with reflist nodes pointing to the places x, y, and z had
  2840.    appeared.  So the arglist is just convenience data passed
  2841.    between these two routines.  It is not kept around after
  2842.    the current #define has been processed and entered into the
  2843.    hash table. */
  2844.  
  2845. struct arglist {
  2846.   struct arglist *next;
  2847.   U_CHAR *name;
  2848.   int length;
  2849.   int argno;
  2850. };
  2851.  
  2852. /* Process a #define command.
  2853. BUF points to the contents of the #define command, as a continguous string.
  2854. LIMIT points to the first character past the end of the definition.
  2855. KEYWORD is the keyword-table entry for #define.  */
  2856.  
  2857. do_define (buf, limit, op, keyword)
  2858.      U_CHAR *buf, *limit;
  2859.      FILE_BUF *op;
  2860.      struct directive *keyword;
  2861. {
  2862.   U_CHAR *bp;            /* temp ptr into input buffer */
  2863.   U_CHAR *symname;        /* remember where symbol name starts */
  2864.   int sym_length;        /* and how long it is */
  2865.  
  2866.   DEFINITION *defn;
  2867.   int arglengths = 0;        /* Accumulate lengths of arg names
  2868.                    plus number of args.  */
  2869.   int hashcode;
  2870.  
  2871.   bp = buf;
  2872.  
  2873.   while (is_hor_space[*bp])
  2874.     bp++;
  2875.  
  2876.   symname = bp;            /* remember where it starts */
  2877.   while (is_idchar[*bp] && bp < limit) {
  2878.     bp++;
  2879.   }
  2880.   sym_length = bp - symname;
  2881.   if (sym_length == 0)
  2882.     error ("invalid macro name");
  2883.   else if (!is_idstart[*symname]) {
  2884.     U_CHAR *msg;            /* what pain... */
  2885.     msg = (U_CHAR *) alloca (sym_length + 1);
  2886.     bcopy (symname, msg, sym_length);
  2887.     msg[sym_length] = 0;
  2888.     error ("invalid macro name `%s'", msg);
  2889.   } else {
  2890.     if (! strncmp (symname, "defined", 7) && sym_length == 7)
  2891.       error ("defining `defined' as a macro");
  2892.   }
  2893.  
  2894.   /* lossage will occur if identifiers or control keywords are broken
  2895.      across lines using backslash.  This is not the right place to take
  2896.      care of that. */
  2897.  
  2898.   if (*bp == '(') {
  2899.     struct arglist *arg_ptrs = NULL;
  2900.     int argno = 0;
  2901.  
  2902.     bp++;            /* skip '(' */
  2903.     SKIP_WHITE_SPACE (bp);
  2904.  
  2905.     /* Loop over macro argument names.  */
  2906.     while (*bp != ')') {
  2907.       struct arglist *temp;
  2908.  
  2909.       temp = (struct arglist *) alloca (sizeof (struct arglist));
  2910.       temp->name = bp;
  2911.       temp->next = arg_ptrs;
  2912.       temp->argno = argno++;
  2913.       arg_ptrs = temp;
  2914.  
  2915.       if (!is_idstart[*bp])
  2916.     warning ("parameter name starts with a digit in #define");
  2917.  
  2918.       /* Find the end of the arg name.  */
  2919.       while (is_idchar[*bp]) {
  2920.     bp++;
  2921.       }
  2922.       temp->length = bp - temp->name;
  2923.       arglengths += temp->length + 2;
  2924.       SKIP_WHITE_SPACE (bp);
  2925.       if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
  2926.     error ("badly punctuated parameter list in #define");
  2927.     goto nope;
  2928.       }
  2929.       if (*bp == ',') {
  2930.     bp++;
  2931.     SKIP_WHITE_SPACE (bp);
  2932.       }
  2933.       if (bp >= limit) {
  2934.     error ("unterminated parameter list in #define");
  2935.     goto nope;
  2936.       }
  2937.     }
  2938.  
  2939.     ++bp;            /* skip paren */
  2940.     /* Skip exactly one space or tab if any.  */
  2941.     if (bp < limit && (*bp == ' ' || *bp == '\t')) ++bp;
  2942.     /* now everything from bp before limit is the definition. */
  2943.     defn = collect_expansion (bp, limit, argno, arg_ptrs);
  2944.  
  2945.     /* Now set defn->argnames to the result of concatenating
  2946.        the argument names in reverse order
  2947.        with comma-space between them.  */
  2948.     defn->argnames = (U_CHAR *) xmalloc (arglengths + 1);
  2949.     {
  2950.       struct arglist *temp;
  2951.       int i = 0;
  2952.       for (temp = arg_ptrs; temp; temp = temp->next) {
  2953.     bcopy (temp->name, &defn->argnames[i], temp->length);
  2954.     i += temp->length;
  2955.     if (temp->next != 0) {
  2956.       defn->argnames[i++] = ',';
  2957.       defn->argnames[i++] = ' ';
  2958.     }
  2959.       }
  2960.       defn->argnames[i] = 0;
  2961.     }
  2962.   } else {
  2963.     /* simple expansion or empty definition; gobble it */
  2964.     if (is_hor_space[*bp])
  2965.       ++bp;        /* skip exactly one blank/tab char */
  2966.     /* now everything from bp before limit is the definition. */
  2967.     defn = collect_expansion (bp, limit, -1, 0);
  2968.     defn->argnames = (U_CHAR *) "";
  2969.   }
  2970.  
  2971.   hashcode = hashf (symname, sym_length, HASHSIZE);
  2972.  
  2973.   {
  2974.     HASHNODE *hp;
  2975.     if ((hp = lookup (symname, sym_length, hashcode)) != NULL) {
  2976.       if (hp->type != T_MACRO
  2977.       || compare_defs (defn, hp->value.defn)) {
  2978.     U_CHAR *msg;            /* what pain... */
  2979.     msg = (U_CHAR *) alloca (sym_length + 20);
  2980.     bcopy (symname, msg, sym_length);
  2981.     strcpy ((char *) (msg + sym_length), " redefined");
  2982.     warning (msg);
  2983.       }
  2984.       /* Replace the old definition.  */
  2985.       hp->type = T_MACRO;
  2986.       hp->value.defn = defn;
  2987.     } else
  2988.       install (symname, sym_length, T_MACRO, defn, hashcode);
  2989.   }
  2990.  
  2991.   return 0;
  2992.  
  2993. nope:
  2994.  
  2995.   return 1;
  2996. }
  2997.  
  2998. /*
  2999.  * return zero if two DEFINITIONs are isomorphic
  3000.  */
  3001. int
  3002. compare_defs (d1, d2)
  3003.      DEFINITION *d1, *d2;
  3004. {
  3005.   register struct reflist *a1, *a2;
  3006.   register U_CHAR *p1 = d1->expansion;
  3007.   register U_CHAR *p2 = d2->expansion;
  3008.   int first = 1;
  3009.  
  3010.   if (d1->nargs != d2->nargs)
  3011.     return 1;
  3012.   if (strcmp ((char *)d1->argnames, (char *)d2->argnames))
  3013.     return 1;
  3014.   for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
  3015.        a1 = a1->next, a2 = a2->next) {
  3016.     if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
  3017.       || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
  3018.     || a1->argno != a2->argno
  3019.     || a1->stringify != a2->stringify
  3020.     || a1->raw_before != a2->raw_before
  3021.     || a1->raw_after != a2->raw_after)
  3022.       return 1;
  3023.     first = 0;
  3024.     p1 += a1->nchars;
  3025.     p2 += a2->nchars;
  3026.   }
  3027.   if (a1 != a2)
  3028.     return 1;
  3029.   if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
  3030.              p2, d2->length - (p2 - d2->expansion), 1))
  3031.     return 1;
  3032.   return 0;
  3033. }
  3034.  
  3035. /* Return 1 if two parts of two macro definitions are effectively different.
  3036.    One of the parts starts at BEG1 and has LEN1 chars;
  3037.    the other has LEN2 chars at BEG2.
  3038.    Any sequence of whitespace matches any other sequence of whitespace.
  3039.    FIRST means these parts are the first of a macro definition;
  3040.     so ignore leading whitespace entirely.
  3041.    LAST means these parts are the last of a macro definition;
  3042.     so ignore trailing whitespace entirely.  */
  3043.  
  3044. comp_def_part (first, beg1, len1, beg2, len2, last)
  3045.      int first;
  3046.      U_CHAR *beg1, *beg2;
  3047.      int len1, len2;
  3048.      int last;
  3049. {
  3050.   register U_CHAR *end1 = beg1 + len1;
  3051.   register U_CHAR *end2 = beg2 + len2;
  3052.   if (first) {
  3053.     while (beg1 != end1 && is_space[*beg1]) beg1++;
  3054.     while (beg2 != end2 && is_space[*beg2]) beg2++;
  3055.   }
  3056.   if (last) {
  3057.     while (beg1 != end1 && is_space[end1[-1]]) end1--;
  3058.     while (beg2 != end2 && is_space[end2[-1]]) end2--;
  3059.   }
  3060.   while (beg1 != end1 && beg2 != end2) {
  3061.     if (is_space[*beg1] && is_space[*beg2]) {
  3062.       while (beg1 != end1 && is_space[*beg1]) beg1++;
  3063.       while (beg2 != end2 && is_space[*beg2]) beg2++;
  3064.     } else if (*beg1 == *beg2) {
  3065.       beg1++; beg2++;
  3066.     } else break;
  3067.   }
  3068.   return (beg1 != end1) || (beg2 != end2);
  3069. }
  3070.  
  3071. /* Read a replacement list for a macro with parameters.
  3072.    Build the DEFINITION structure.
  3073.    Reads characters of text starting at BUF until LIMIT.
  3074.    ARGLIST specifies the formal parameters to look for
  3075.    in the text of the definition; NARGS is the number of args
  3076.    in that list, or -1 for a macro name that wants no argument list.
  3077.    MACRONAME is the macro name itself (so we can avoid recursive expansion)
  3078.    and NAMELEN is its length in characters.
  3079.    
  3080. Note that comments and backslash-newlines have already been deleted
  3081. from the argument.  */
  3082.  
  3083. /* Leading and trailing Space, Tab, etc. are converted to markers
  3084.    Newline Space, Newline Tab, etc.
  3085.    Newline Space makes a space in the final output
  3086.    but is discarded if stringified.  (Newline Tab is similar but
  3087.    makes a Tab instead.)
  3088.  
  3089.    If there is no trailing whitespace, a Newline Space is added at the end
  3090.    to prevent concatenation that would be contrary to the standard.  */
  3091.  
  3092. DEFINITION *
  3093. collect_expansion (buf, end, nargs, arglist)
  3094.      U_CHAR *buf, *end;
  3095.      int nargs;
  3096.      struct arglist *arglist;
  3097. {
  3098.   DEFINITION *defn;
  3099.   register U_CHAR *p, *limit, *lastp, *exp_p;
  3100.   struct reflist *endpat = NULL;
  3101.   /* Pointer to first nonspace after last ## seen.  */
  3102.   U_CHAR *concat = 0;
  3103.   /* Pointer to first nonspace after last single-# seen.  */
  3104.   U_CHAR *stringify = 0;
  3105.   int maxsize;
  3106.   int expected_delimiter = '\0';
  3107.  
  3108.   /* Scan thru the replacement list, ignoring comments and quoted
  3109.      strings, picking up on the macro calls.  It does a linear search
  3110.      thru the arg list on every potential symbol.  Profiling might say
  3111.      that something smarter should happen. */
  3112.  
  3113.   if (end < buf)
  3114.     abort ();
  3115.  
  3116.   /* Find the beginning of the trailing whitespace.  */
  3117.   /* Find end of leading whitespace.  */
  3118.   limit = end;
  3119.   p = buf;
  3120.   while (p < limit && is_space[limit[-1]]) limit--;
  3121.   while (p < limit && is_space[*p]) p++;
  3122.  
  3123.   /* Allocate space for the text in the macro definition.
  3124.      Leading and trailing whitespace chars need 2 bytes each.
  3125.      Each other input char may or may not need 1 byte,
  3126.      so this is an upper bound.
  3127.      The extra 2 are for invented trailing newline-marker and final null.  */
  3128.   maxsize = (sizeof (DEFINITION)
  3129.          + 2 * (end - limit) + 2 * (p - buf)
  3130.          + (limit - p) + 3);
  3131.   defn = (DEFINITION *) xcalloc (1, maxsize);
  3132.  
  3133.   defn->nargs = nargs;
  3134.   exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
  3135.   lastp = exp_p;
  3136.  
  3137.   p = buf;
  3138.  
  3139.   /* Convert leading whitespace to Newline-markers.  */
  3140.   while (p < limit && is_space[*p]) {
  3141.     *exp_p++ = '\n';
  3142.     *exp_p++ = *p++;
  3143.   }
  3144.  
  3145.   if (p + 1 < limit && p[0] == '#' && p[1] == '#') {
  3146.     error ("## operator at start of macro definition");
  3147.     p += 2;
  3148.   }
  3149.  
  3150.   /* Process the main body of the definition.  */
  3151.   while (p < limit) {
  3152.     int skipped_arg = 0;
  3153.     register U_CHAR c = *p++;
  3154.  
  3155.     *exp_p++ = c;
  3156.  
  3157.     if (!traditional) {
  3158.       switch (c) {
  3159.       case '\'':
  3160.       case '\"':
  3161.     for (; p < limit && *p != c; p++) {
  3162.       *exp_p++ = *p;
  3163.       if (*p == '\\') {
  3164.         *exp_p++ = *++p;
  3165.       }
  3166.     }
  3167.     *exp_p++ = *p++;
  3168.     break;
  3169.  
  3170.     /* Special hack: if a \# is written in the #define
  3171.        include a # in the definition.  This is useless for C code
  3172.        but useful for preprocessing other things.  */
  3173.  
  3174.       case '\\':
  3175.     if (p < limit && *p == '#') {
  3176.       /* Pass through this # */
  3177.       exp_p--;
  3178.       *exp_p++ = *p++;
  3179.     } else if (p < limit) {
  3180.       /* Otherwise backslash goes through but makes next char ordinary.  */
  3181.       *exp_p++ = *p++;
  3182.     }
  3183.     break;
  3184.  
  3185.       case '#':
  3186.     if (p < limit && *p == '#') {
  3187.       /* ##: concatenate preceding and following tokens.  */
  3188.       /* Take out the first #, discard preceding whitespace.  */
  3189.       exp_p--;
  3190.       while (exp_p > lastp && is_hor_space[exp_p[-1]])
  3191.         --exp_p;
  3192.       /* Skip the second #.  */
  3193.       p++;
  3194.       /* Discard following whitespace.  */
  3195.       SKIP_WHITE_SPACE (p);
  3196.       concat = p;
  3197.       if (limit <= p)
  3198.         error ("## operator at end of macro definition");
  3199.     } else {
  3200.       /* Single #: stringify following argument ref.
  3201.          Don't leave the # in the expansion.  */
  3202.       exp_p--;
  3203.       SKIP_WHITE_SPACE (p);
  3204.       if (p == limit || ! is_idstart[*p] || nargs <= 0)
  3205.         error ("# operator should be followed by a macro argument name");
  3206.       else
  3207.         stringify = p;
  3208.     }
  3209.     break;
  3210.       }
  3211.     } else {
  3212.       /* In -traditional mode, recognize arguments inside strings and
  3213.      and character constants, and ignore special properties of #.
  3214.      Arguments inside strings are considered "stringified", but no
  3215.      extra quote marks are supplied.  */
  3216.       switch (c) {
  3217.       case '\'':
  3218.       case '\"':
  3219.     if (expected_delimiter != '\0') {
  3220.       if (c == expected_delimiter)
  3221.         expected_delimiter = '\0';
  3222.     } else
  3223.       expected_delimiter = c;
  3224.     break;
  3225.  
  3226.       case '\\':
  3227.     /* Backslash quotes delimiters and itself, but not macro args.  */
  3228.     if (expected_delimiter != 0 && p < limit
  3229.         && (*p == expected_delimiter || *p == '\\')) {
  3230.       *exp_p++ = *p++;
  3231.       continue;
  3232.     }
  3233.     break;
  3234.  
  3235.       case '/':
  3236.     if (expected_delimiter != '\0') /* No comments inside strings.  */
  3237.       break;
  3238.     if (*p == '*') {
  3239.       /* If we find a comment that wasn't removed by handle_directive,
  3240.          this must be -traditional.  So replace the comment with
  3241.          nothing at all.  */
  3242.       exp_p--;
  3243.       p += 1;
  3244.       while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
  3245.         p++;
  3246. #if 0
  3247.       /* Mark this as a concatenation-point, as if it had been ##.  */
  3248.       concat = p;
  3249. #endif
  3250.     }
  3251.     break;
  3252.       }
  3253.     }
  3254.  
  3255.     if (is_idchar[c] && nargs > 0) {
  3256.       U_CHAR *id_beg = p - 1;
  3257.       int id_len;
  3258.  
  3259.       --exp_p;
  3260.       while (p != limit && is_idchar[*p]) p++;
  3261.       id_len = p - id_beg;
  3262.  
  3263.       if (is_idstart[c]) {
  3264.     register struct arglist *arg;
  3265.  
  3266.     for (arg = arglist; arg != NULL; arg = arg->next) {
  3267.       struct reflist *tpat;
  3268.  
  3269.       if (arg->name[0] == c
  3270.           && arg->length == id_len
  3271.           && strncmp (arg->name, id_beg, id_len) == 0) {
  3272.         /* make a pat node for this arg and append it to the end of
  3273.            the pat list */
  3274.         tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
  3275.         tpat->next = NULL;
  3276.         tpat->raw_before = concat == id_beg;
  3277.         tpat->raw_after = 0;
  3278.         tpat->stringify = (traditional ? expected_delimiter != '\0'
  3279.                    : stringify == id_beg);
  3280.  
  3281.         if (endpat == NULL)
  3282.           defn->pattern = tpat;
  3283.         else
  3284.           endpat->next = tpat;
  3285.         endpat = tpat;
  3286.  
  3287.         tpat->argno = arg->argno;
  3288.         tpat->nchars = exp_p - lastp;
  3289.         {
  3290.           register U_CHAR *p1 = p;
  3291.           SKIP_WHITE_SPACE (p1);
  3292.           if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
  3293.         tpat->raw_after = 1;
  3294.         }
  3295.         lastp = exp_p;    /* place to start copying from next time */
  3296.         skipped_arg = 1;
  3297.         break;
  3298.       }
  3299.     }
  3300.       }
  3301.  
  3302.       /* If this was not a macro arg, copy it into the expansion.  */
  3303.       if (! skipped_arg) {
  3304.     register U_CHAR *lim1 = p;
  3305.     p = id_beg;
  3306.     while (p != lim1)
  3307.       *exp_p++ = *p++;
  3308.     if (stringify == id_beg)
  3309.       error ("# operator should be followed by a macro argument name");
  3310.       }
  3311.     }
  3312.   }
  3313.  
  3314.   if (limit < end) {
  3315.     /* Convert trailing whitespace to Newline-markers.  */
  3316.     while (limit < end && is_space[*limit]) {
  3317.       *exp_p++ = '\n';
  3318.       *exp_p++ = *limit++;
  3319.     }
  3320.   } else if (!traditional) {
  3321.     /* There is no trailing whitespace, so invent some.  */
  3322.     *exp_p++ = '\n';
  3323.     *exp_p++ = ' ';
  3324.   }
  3325.  
  3326.   *exp_p = '\0';
  3327.  
  3328.   defn->length = exp_p - defn->expansion;
  3329.  
  3330.   /* Crash now if we overrun the allocated size.  */
  3331.   if (defn->length + 1 > maxsize)
  3332.     abort ();
  3333.  
  3334. #if 0
  3335. /* This isn't worth the time it takes.  */
  3336.   /* give back excess storage */
  3337.   defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
  3338. #endif
  3339.  
  3340.   return defn;
  3341. }
  3342.  
  3343. /*
  3344.  * interpret #line command.  Remembers previously seen fnames
  3345.  * in its very own hash table.
  3346.  */
  3347. #define FNAME_HASHSIZE 37
  3348.  
  3349. do_line (buf, limit, op, keyword)
  3350.      U_CHAR *buf, *limit;
  3351.      FILE_BUF *op;
  3352.      struct directive *keyword;
  3353. {
  3354.   register U_CHAR *bp;
  3355.   FILE_BUF *ip = &instack[indepth];
  3356.   FILE_BUF tem;
  3357.   int new_lineno;
  3358.   enum file_change_code file_change = same_file;
  3359.  
  3360.   /* Expand any macros.  */
  3361.   tem = expand_to_temp_buffer (buf, limit, 0);
  3362.  
  3363.   /* Point to macroexpanded line, which is null-terminated now.  */
  3364.   bp = tem.buf;
  3365.   SKIP_WHITE_SPACE (bp);
  3366.  
  3367.   if (!isdigit (*bp)) {
  3368.     error ("invalid format #line command");
  3369.     return;
  3370.   }
  3371.  
  3372.   /* The Newline at the end of this line remains to be processed.
  3373.      To put the next line at the specified line number,
  3374.      we must store a line number now that is one less.  */
  3375.   new_lineno = atoi (bp) - 1;
  3376.  
  3377.   /* skip over the line number.  */
  3378.   while (isdigit (*bp))
  3379.     bp++;
  3380.  
  3381. #if 0 /* #line 10"foo.c" is supposed to be allowed.  */
  3382.   if (*bp && !is_space[*bp]) {
  3383.     error ("invalid format #line command");
  3384.     return;
  3385.   }
  3386. #endif
  3387.  
  3388.   SKIP_WHITE_SPACE (bp);
  3389.  
  3390.   if (*bp == '\"') {
  3391.     static HASHNODE *fname_table[FNAME_HASHSIZE];
  3392.     HASHNODE *hp, **hash_bucket;
  3393.     U_CHAR *fname;
  3394.     int fname_length;
  3395.  
  3396.     fname = ++bp;
  3397.  
  3398.     while (*bp && *bp != '\"')
  3399.       bp++;
  3400.     if (*bp != '\"') {
  3401.       error ("invalid format #line command");
  3402.       return;
  3403.     }
  3404.  
  3405.     fname_length = bp - fname;
  3406.  
  3407.     bp++;
  3408.     SKIP_WHITE_SPACE (bp);
  3409.     if (*bp) {
  3410.       if (*bp == '1')
  3411.     file_change = enter_file;
  3412.       else if (*bp == '2')
  3413.     file_change = leave_file;
  3414.       else {
  3415.     error ("invalid format #line command");
  3416.     return;
  3417.       }
  3418.  
  3419.       bp++;
  3420.       SKIP_WHITE_SPACE (bp);
  3421.       if (*bp) {
  3422.     error ("invalid format #line command");
  3423.     return;
  3424.       }
  3425.     }
  3426.  
  3427.     hash_bucket =
  3428.       &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
  3429.     for (hp = *hash_bucket; hp != NULL; hp = hp->next)
  3430.       if (hp->length == fname_length &&
  3431.       strncmp (hp->value.cpval, fname, fname_length) == 0) {
  3432.     ip->fname = hp->value.cpval;
  3433.     break;
  3434.       }
  3435.     if (hp == 0) {
  3436.       /* Didn't find it; cons up a new one.  */
  3437.       hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
  3438.       hp->next = *hash_bucket;
  3439.       *hash_bucket = hp;
  3440.  
  3441.       hp->length = fname_length;
  3442.       ip->fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
  3443.       bcopy (fname, hp->value.cpval, fname_length);
  3444.     }
  3445.   } else if (*bp) {
  3446.     error ("invalid format #line command");
  3447.     return;
  3448.   }
  3449.  
  3450.   ip->lineno = new_lineno;
  3451.   output_line_command (ip, op, 0, file_change);
  3452.   check_expand (op, ip->length - (ip->bufp - ip->buf));
  3453. }
  3454.  
  3455. /*
  3456.  * remove all definitions of symbol from symbol table.
  3457.  * according to un*x /lib/cpp, it is not an error to undef
  3458.  * something that has no definitions, so it isn't one here either.
  3459.  */
  3460. do_undef (buf, limit, op, keyword)
  3461.      U_CHAR *buf, *limit;
  3462.      FILE_BUF *op;
  3463.      struct directive *keyword;
  3464. {
  3465.   HASHNODE *hp;
  3466.  
  3467.   SKIP_WHITE_SPACE (buf);
  3468.  
  3469.   if (! strncmp (buf, "defined", 7) && ! is_idchar[buf[7]])
  3470.     warning ("undefining `defined'");
  3471.  
  3472.   while ((hp = lookup (buf, -1, -1)) != NULL) {
  3473.     if (hp->type != T_MACRO)
  3474.       warning ("undefining `%s'", hp->name);
  3475.     delete_macro (hp);
  3476.   }
  3477. }
  3478.  
  3479. /*
  3480.  * Report a fatal error detected by the program we are processing.
  3481.  * Use the text of the line in the error message, then terminate.
  3482.  * (We use error() because it prints the filename & line#.)
  3483.  */
  3484. do_error (buf, limit, op, keyword)
  3485.      U_CHAR *buf, *limit;
  3486.      FILE_BUF *op;
  3487.      struct directive *keyword;
  3488. {
  3489.   int length = limit - buf;
  3490.   char *copy = (char *) xmalloc (length + 1);
  3491.   bcopy (buf, copy, length);
  3492.   copy[length] = 0;
  3493.   SKIP_WHITE_SPACE (copy);
  3494.   error ("#error %s", copy);
  3495.   exit (FATAL_EXIT_CODE);
  3496. }
  3497.  
  3498. /* Remember the name of the current file being read from so that we can
  3499.    avoid ever including it again.  */
  3500.  
  3501. do_once ()
  3502. {
  3503.   int i;
  3504.   FILE_BUF *ip = NULL;
  3505.  
  3506.   for (i = indepth; i >= 0; i--)
  3507.     if (instack[i].fname != NULL) {
  3508.       ip = &instack[i];
  3509.       break;
  3510.     }
  3511.  
  3512.   if (ip != NULL)
  3513.     {
  3514.       struct file_name_list *new;
  3515.  
  3516.       new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  3517.       new->next = dont_repeat_files;
  3518.       dont_repeat_files = new;
  3519.       new->fname = savestring (ip->fname);
  3520.     }
  3521. }
  3522.  
  3523. /* #pragma and its argument line have already been copied to the output file.
  3524.    Here just check for recognized pragmas.  */
  3525.  
  3526. do_pragma (buf, limit)
  3527.      U_CHAR *buf, *limit;
  3528. {
  3529.   while (*buf == ' ' || *buf == '\t')
  3530.     buf++;
  3531.   if (!strncmp (buf, "once", 4))
  3532.     do_once ();
  3533. }
  3534.  
  3535. #if 0
  3536. /* This was a fun hack, but #pragma seems to start to be useful.
  3537.    By failing to recognize it, we pass it through unchanged to cc1.  */
  3538.  
  3539. /*
  3540.  * the behavior of the #pragma directive is implementation defined.
  3541.  * this implementation defines it as follows.
  3542.  */
  3543. do_pragma ()
  3544. {
  3545.   close (0);
  3546.   if (open ("/dev/tty", O_RDONLY, 0666) != 0)
  3547.     goto nope;
  3548.   close (1);
  3549.   if (open ("/dev/tty", O_WRONLY, 0666) != 1)
  3550.     goto nope;
  3551.   execl ("/usr/games/hack", "#pragma", 0);
  3552.   execl ("/usr/games/rogue", "#pragma", 0);
  3553.   execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
  3554.   execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
  3555. nope:
  3556.   fatal ("You are in a maze of twisty compiler features, all different");
  3557. }
  3558. #endif
  3559.  
  3560. /* Just ignore #sccs, on systems where we define it at all.  */
  3561. do_sccs ()
  3562. {
  3563.   if (pedantic)
  3564.     error ("ANSI C does not allow #sccs");
  3565. }
  3566.  
  3567. /*
  3568.  * handle #if command by
  3569.  *   1) inserting special `defined' keyword into the hash table
  3570.  *    that gets turned into 0 or 1 by special_symbol (thus,
  3571.  *    if the luser has a symbol called `defined' already, it won't
  3572.  *      work inside the #if command)
  3573.  *   2) rescan the input into a temporary output buffer
  3574.  *   3) pass the output buffer to the yacc parser and collect a value
  3575.  *   4) clean up the mess left from steps 1 and 2.
  3576.  *   5) call conditional_skip to skip til the next #endif (etc.),
  3577.  *      or not, depending on the value from step 3.
  3578.  */
  3579.  
  3580. do_if (buf, limit, op, keyword)
  3581.      U_CHAR *buf, *limit;
  3582.      FILE_BUF *op;
  3583.      struct directive *keyword;
  3584. {
  3585.   int value;
  3586.   FILE_BUF *ip = &instack[indepth];
  3587.  
  3588.   value = eval_if_expression (buf, limit - buf);
  3589.   conditional_skip (ip, value == 0, T_IF);
  3590. }
  3591.  
  3592. /*
  3593.  * handle a #elif directive by not changing  if_stack  either.
  3594.  * see the comment above do_else.
  3595.  */
  3596.  
  3597. do_elif (buf, limit, op, keyword)
  3598.      U_CHAR *buf, *limit;
  3599.      FILE_BUF *op;
  3600.      struct directive *keyword;
  3601. {
  3602.   int value;
  3603.   FILE_BUF *ip = &instack[indepth];
  3604.  
  3605.   if (if_stack == instack[indepth].if_stack) {
  3606.     error ("#elif not within a conditional");
  3607.     return;
  3608.   } else {
  3609.     if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
  3610.       error ("#elif after #else");
  3611.       fprintf (stderr, " (matches line %d", if_stack->lineno);
  3612.       if (if_stack->fname != NULL && ip->fname != NULL &&
  3613.       strcmp (if_stack->fname, ip->fname) != 0)
  3614.     fprintf (stderr, ", file %s", if_stack->fname);
  3615.       fprintf (stderr, ")\n");
  3616.     }
  3617.     if_stack->type = T_ELIF;
  3618.   }
  3619.  
  3620.   if (if_stack->if_succeeded)
  3621.     skip_if_group (ip, 0);
  3622.   else {
  3623.     value = eval_if_expression (buf, limit - buf);
  3624.     if (value == 0)
  3625.       skip_if_group (ip, 0);
  3626.     else {
  3627.       ++if_stack->if_succeeded;    /* continue processing input */
  3628.       output_line_command (ip, op, 1, same_file);
  3629.     }
  3630.   }
  3631. }
  3632.  
  3633. /*
  3634.  * evaluate a #if expression in BUF, of length LENGTH,
  3635.  * then parse the result as a C expression and return the value as an int.
  3636.  */
  3637. int
  3638. eval_if_expression (buf, length)
  3639.      U_CHAR *buf;
  3640.      int length;
  3641. {
  3642.   FILE_BUF temp_obuf;
  3643.   HASHNODE *save_defined;
  3644.   int value;
  3645.  
  3646.   save_defined = install ("defined", -1, T_SPEC_DEFINED, 0, -1);
  3647.   temp_obuf = expand_to_temp_buffer (buf, buf + length, 0);
  3648.   delete_macro (save_defined);    /* clean up special symbol */
  3649.  
  3650.   value = parse_c_expression (temp_obuf.buf);
  3651.  
  3652.   free (temp_obuf.buf);
  3653.  
  3654.   return value;
  3655. }
  3656.  
  3657. /*
  3658.  * routine to handle ifdef/ifndef.  Try to look up the symbol,
  3659.  * then do or don't skip to the #endif/#else/#elif depending
  3660.  * on what directive is actually being processed.
  3661.  */
  3662. do_xifdef (buf, limit, op, keyword)
  3663.      U_CHAR *buf, *limit;
  3664.      FILE_BUF *op;
  3665.      struct directive *keyword;
  3666. {
  3667.   int skip;
  3668.   FILE_BUF *ip = &instack[indepth];
  3669.   U_CHAR *end; 
  3670.  
  3671.   /* Discard leading and trailing whitespace.  */
  3672.   SKIP_WHITE_SPACE (buf);
  3673.   while (limit != buf && is_hor_space[limit[-1]]) limit--;
  3674.  
  3675.   /* Find the end of the identifier at the beginning.  */
  3676.   for (end = buf; is_idchar[*end]; end++);
  3677.  
  3678.   if (end == buf) {
  3679.     skip = (keyword->type == T_IFDEF);
  3680.     if (! traditional)
  3681.       warning (end == limit ? "#%s with no argument"
  3682.            : "#%s argument starts with punctuation",
  3683.            keyword->name);
  3684.   } else {
  3685.     if (pedantic && buf[0] >= '0' && buf[0] <= '9')
  3686.       warning ("#%s argument starts with a digit", keyword->name);
  3687.     else if (end != limit && !traditional)
  3688.       warning ("garbage at end of #%s argument", keyword->name);
  3689.  
  3690.     skip = (lookup (buf, end-buf, -1) == NULL) ^ (keyword->type == T_IFNDEF);
  3691.   }
  3692.  
  3693.   conditional_skip (ip, skip, T_IF);
  3694. }
  3695.  
  3696. /*
  3697.  * push TYPE on stack; then, if SKIP is nonzero, skip ahead.
  3698.  */
  3699. void
  3700. conditional_skip (ip, skip, type)
  3701.      FILE_BUF *ip;
  3702.      int skip;
  3703.      enum node_type type;
  3704. {
  3705.   IF_STACK_FRAME *temp;
  3706.  
  3707.   temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
  3708.   temp->fname = ip->fname;
  3709.   temp->lineno = ip->lineno;
  3710.   temp->next = if_stack;
  3711.   if_stack = temp;
  3712.  
  3713.   if_stack->type = type;
  3714.  
  3715.   if (skip != 0) {
  3716.     skip_if_group (ip, 0);
  3717.     return;
  3718.   } else {
  3719.     ++if_stack->if_succeeded;
  3720.     output_line_command (ip, &outbuf, 1, same_file);
  3721.   }
  3722. }
  3723.  
  3724. /*
  3725.  * skip to #endif, #else, or #elif.  adjust line numbers, etc.
  3726.  * leaves input ptr at the sharp sign found.
  3727.  * If ANY is nonzero, return at next directive of any sort.
  3728.  */
  3729. void
  3730. skip_if_group (ip, any)
  3731.      FILE_BUF *ip;
  3732.      int any;
  3733. {
  3734.   register U_CHAR *bp = ip->bufp, *cp;
  3735.   register U_CHAR *endb = ip->buf + ip->length;
  3736.   struct directive *kt;
  3737.   IF_STACK_FRAME *save_if_stack = if_stack; /* don't pop past here */
  3738.   U_CHAR *beg_of_line = bp;
  3739.  
  3740.   while (bp < endb) {
  3741.     switch (*bp++) {
  3742.     case '/':            /* possible comment */
  3743.       if (*bp == '\\' && bp[1] == '\n')
  3744.     newline_fix (bp);
  3745.       if (*bp == '*'
  3746.       || (cplusplus && *bp == '/')) {
  3747.     ip->bufp = ++bp;
  3748.     bp = skip_to_end_of_comment (ip, &ip->lineno);
  3749.       }
  3750.       break;
  3751.     case '\"':
  3752.     case '\'':
  3753.       bp = skip_quoted_string (bp - 1, endb, ip->lineno, &ip->lineno, 0, 0);
  3754.       break;
  3755.     case '\\':
  3756.       /* Char after backslash loses its special meaning.  */
  3757.       if (bp < endb) {
  3758.     if (*bp == '\n')
  3759.       ++ip->lineno;        /* But do update the line-count.  */
  3760.     bp++;
  3761.       }
  3762.       break;
  3763.     case '\n':
  3764.       ++ip->lineno;
  3765.       beg_of_line = bp;
  3766.       break;
  3767.     case '#':
  3768.       ip->bufp = bp - 1;
  3769.  
  3770.       /* # keyword: a # must be first nonblank char on the line */
  3771.       if (beg_of_line == 0)
  3772.     break;
  3773.       /* Scan from start of line, skipping whitespace, comments
  3774.      and backslash-newlines, and see if we reach this #.
  3775.      If not, this # is not special.  */
  3776.       bp = beg_of_line;
  3777.       while (1) {
  3778.     if (is_hor_space[*bp])
  3779.       bp++;
  3780.     else if (*bp == '\\' && bp[1] == '\n')
  3781.       bp += 2;
  3782.     else if (*bp == '/' && bp[1] == '*') {
  3783.       bp += 2;
  3784.       while (!(*bp == '*' && bp[1] == '/'))
  3785.         bp++;
  3786.       bp += 2;
  3787.     }
  3788.     else if (cplusplus && *bp == '/' && bp[1] == '/') {
  3789.       bp += 2;
  3790.       while (*bp++ != '\n') ;
  3791.         }
  3792.     else break;
  3793.       }
  3794.       if (bp != ip->bufp) {
  3795.     bp = ip->bufp + 1;    /* Reset bp to after the #.  */
  3796.     break;
  3797.       }
  3798.  
  3799.       bp = ip->bufp + 1;        /* point at '#' */
  3800.  
  3801.       /* Skip whitespace and \-newline.  */
  3802.       while (1) {
  3803.     if (is_hor_space[*bp])
  3804.       bp++;
  3805.     else if (*bp == '\\' && bp[1] == '\n')
  3806.       bp += 2;
  3807.     else break;
  3808.       }
  3809.  
  3810.       cp = bp;
  3811.  
  3812.       /* Now find end of directive name.
  3813.      If we encounter a backslash-newline, exchange it with any following
  3814.      symbol-constituents so that we end up with a contiguous name.  */
  3815.  
  3816.       while (1) {
  3817.     if (is_idchar[*bp])
  3818.       bp++;
  3819.     else {
  3820.       if (*bp == '\\' && bp[1] == '\n')
  3821.         name_newline_fix (bp);
  3822.       if (is_idchar[*bp])
  3823.         bp++;
  3824.       else break;
  3825.     }
  3826.       }
  3827.  
  3828.       for (kt = directive_table; kt->length >= 0; kt++) {
  3829.     IF_STACK_FRAME *temp;
  3830.     if (strncmp (cp, kt->name, kt->length) == 0
  3831.         && !is_idchar[cp[kt->length]]) {
  3832.  
  3833.       /* If we are asked to return on next directive,
  3834.          do so now.  */
  3835.       if (any)
  3836.         return;
  3837.  
  3838.       switch (kt->type) {
  3839.       case T_IF:
  3840.       case T_IFDEF:
  3841.       case T_IFNDEF:
  3842.         temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
  3843.         temp->next = if_stack;
  3844.         if_stack = temp;
  3845.         temp->lineno = ip->lineno;
  3846.         temp->fname = ip->fname;
  3847.         temp->type = kt->type;
  3848.         break;
  3849.       case T_ELSE:
  3850.       case T_ENDIF:
  3851.         if (pedantic && if_stack != save_if_stack)
  3852.           validate_else (bp);
  3853.       case T_ELIF:
  3854.         if (if_stack == instack[indepth].if_stack) {
  3855.           error ("#%s not within a conditional", kt->name);
  3856.           break;
  3857.         }
  3858.         else if (if_stack == save_if_stack)
  3859.           return;        /* found what we came for */
  3860.  
  3861.         if (kt->type != T_ENDIF) {
  3862.           if (if_stack->type == T_ELSE)
  3863.         error ("#else or #elif after #else");
  3864.           if_stack->type = kt->type;
  3865.           break;
  3866.         }
  3867.  
  3868.         temp = if_stack;
  3869.         if_stack = if_stack->next;
  3870.         free (temp);
  3871.         break;
  3872.       }
  3873.       break;
  3874.     }
  3875.       }
  3876.     }
  3877.   }
  3878.   ip->bufp = bp;
  3879.   /* after this returns, rescan will exit because ip->bufp
  3880.      now points to the end of the buffer.
  3881.      rescan is responsible for the error message also.  */
  3882. }
  3883.  
  3884. /*
  3885.  * handle a #else directive.  Do this by just continuing processing
  3886.  * without changing  if_stack ;  this is so that the error message
  3887.  * for missing #endif's etc. will point to the original #if.  It
  3888.  * is possible that something different would be better.
  3889.  */
  3890. do_else (buf, limit, op, keyword)
  3891.      U_CHAR *buf, *limit;
  3892.      FILE_BUF *op;
  3893.      struct directive *keyword;
  3894. {
  3895.   FILE_BUF *ip = &instack[indepth];
  3896.  
  3897.   if (pedantic) {
  3898.     SKIP_WHITE_SPACE (buf);
  3899.     if (buf != limit)
  3900.       warning ("text following #else violates ANSI standard");
  3901.   }
  3902.  
  3903.   if (if_stack == instack[indepth].if_stack) {
  3904.     error ("#else not within a conditional");
  3905.     return;
  3906.   } else {
  3907.     if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
  3908.       error ("#else after #else");
  3909.       fprintf (stderr, " (matches line %d", if_stack->lineno);
  3910.       if (strcmp (if_stack->fname, ip->fname) != 0)
  3911.     fprintf (stderr, ", file %s", if_stack->fname);
  3912.       fprintf (stderr, ")\n");
  3913.     }
  3914.     if_stack->type = T_ELSE;
  3915.   }
  3916.  
  3917.   if (if_stack->if_succeeded)
  3918.     skip_if_group (ip, 0);
  3919.   else {
  3920.     ++if_stack->if_succeeded;    /* continue processing input */
  3921.     output_line_command (ip, op, 1, same_file);
  3922.   }
  3923. }
  3924.  
  3925. /*
  3926.  * unstack after #endif command
  3927.  */
  3928. do_endif (buf, limit, op, keyword)
  3929.      U_CHAR *buf, *limit;
  3930.      FILE_BUF *op;
  3931.      struct directive *keyword;
  3932. {
  3933.   if (pedantic) {
  3934.     SKIP_WHITE_SPACE (buf);
  3935.     if (buf != limit)
  3936.       warning ("text following #endif violates ANSI standard");
  3937.   }
  3938.  
  3939.   if (if_stack == instack[indepth].if_stack)
  3940.     error ("unbalanced #endif");
  3941.   else {
  3942.     IF_STACK_FRAME *temp = if_stack;
  3943.     if_stack = if_stack->next;
  3944.     free (temp);
  3945.     output_line_command (&instack[indepth], op, 1, same_file);
  3946.   }
  3947. }
  3948.  
  3949. /* When an #else or #endif is found while skipping failed conditional,
  3950.    if -pedantic was specified, this is called to warn about text after
  3951.    the command name.  P points to the first char after the command name.  */
  3952.  
  3953. validate_else (p)
  3954.      register U_CHAR *p;
  3955. {
  3956.   /* Advance P over whitespace and comments.  */
  3957.   while (1) {
  3958.     if (*p == '\\' && p[1] == '\n')
  3959.       p += 2;
  3960.     if (is_hor_space[*p])
  3961.       p++;
  3962.     else if (*p == '/') {
  3963.       if (p[1] == '\\' && p[2] == '\n')
  3964.     newline_fix (p + 1);
  3965.       if (p[1] == '*') {
  3966.     p += 2;
  3967.     /* Don't bother warning about unterminated comments
  3968.        since that will happen later.  Just be sure to exit.  */
  3969.     while (*p) {
  3970.       if (p[1] == '\\' && p[2] == '\n')
  3971.         newline_fix (p + 1);
  3972.       if (*p == '*' && p[1] == '/') {
  3973.         p += 2;
  3974.         break;
  3975.       }
  3976.       p++;
  3977.     }
  3978.       }
  3979.       else if (cplusplus && p[1] == '/') {
  3980.     p += 2;
  3981.     while (*p && *p++ != '\n') ;
  3982.       }
  3983.     } else break;
  3984.   }
  3985.   if (*p && *p != '\n')
  3986.     warning ("text following #else or #endif violates ANSI standard");
  3987. }
  3988.  
  3989. /*
  3990.  * Skip a comment, assuming the input ptr immediately follows the
  3991.  * initial slash-star.  Bump line counter as necessary.
  3992.  * (The canonical line counter is &ip->lineno).
  3993.  * Don't use this routine (or the next one) if bumping the line
  3994.  * counter is not sufficient to deal with newlines in the string.
  3995.  */
  3996. U_CHAR *
  3997. skip_to_end_of_comment (ip, line_counter)
  3998.      register FILE_BUF *ip;
  3999.      int *line_counter;        /* place to remember newlines, or NULL */
  4000. {
  4001.   register U_CHAR *limit = ip->buf + ip->length;
  4002.   register U_CHAR *bp = ip->bufp;
  4003.   FILE_BUF *op = &outbuf;    /* JF */
  4004.   int output = put_out_comments && !line_counter;
  4005.  
  4006.     /* JF this line_counter stuff is a crock to make sure the
  4007.        comment is only put out once, no matter how many times
  4008.        the comment is skipped.  It almost works */
  4009.   if (output) {
  4010.     *op->bufp++ = '/';
  4011.     *op->bufp++ = '*';
  4012.   }
  4013.   if (cplusplus && bp[-1] == '/') {
  4014.     if (output) {
  4015.       while (bp < limit)
  4016.     if ((*op->bufp++ = *bp++) == '\n') {
  4017.       bp--;
  4018.       break;
  4019.     }
  4020.       op->bufp[-1] = '*';
  4021.       *op->bufp++ = '/';
  4022.       *op->bufp++ = '\n';
  4023.     } else {
  4024.       while (bp < limit) {
  4025.     if (*bp++ == '\n') {
  4026.       bp--;
  4027.       break;
  4028.     }
  4029.       }
  4030.     }
  4031.     ip->bufp = bp;
  4032.     return bp;
  4033.   }
  4034.   while (bp < limit) {
  4035.     if (output)
  4036.       *op->bufp++ = *bp;
  4037.     switch (*bp++) {
  4038.     case '/':
  4039.       if (warn_comments && bp < limit && *bp == '*')
  4040.     warning("`/*' within comment");
  4041.       break;
  4042.     case '\n':
  4043.       if (line_counter != NULL)
  4044.     ++*line_counter;
  4045.       if (output)
  4046.     ++op->lineno;
  4047.       break;
  4048.     case '*':
  4049.       if (*bp == '\\' && bp[1] == '\n')
  4050.     newline_fix (bp);
  4051.       if (*bp == '/') {
  4052.         if (output)
  4053.       *op->bufp++ = '/';
  4054.     ip->bufp = ++bp;
  4055.     return bp;
  4056.       }
  4057.       break;
  4058.     }
  4059.   }
  4060.   ip->bufp = bp;
  4061.   return bp;
  4062. }
  4063.  
  4064. /*
  4065.  * Skip over a quoted string.  BP points to the opening quote.
  4066.  * Returns a pointer after the closing quote.  Don't go past LIMIT.
  4067.  * START_LINE is the line number of the starting point (but it need
  4068.  * not be valid if the starting point is inside a macro expansion).
  4069.  *
  4070.  * The input stack state is not changed.
  4071.  *
  4072.  * If COUNT_NEWLINES is nonzero, it points to an int to increment
  4073.  * for each newline passed.
  4074.  *
  4075.  * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
  4076.  * if we pass a backslash-newline.
  4077.  *
  4078.  * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
  4079.  */
  4080. U_CHAR *
  4081. skip_quoted_string (bp, limit, start_line, count_newlines, backslash_newlines_p, eofp)
  4082.      register U_CHAR *bp;
  4083.      register U_CHAR *limit;
  4084.      int start_line;
  4085.      int *count_newlines;
  4086.      int *backslash_newlines_p;
  4087.      int *eofp;
  4088. {
  4089.   register U_CHAR c, match;
  4090.  
  4091.   match = *bp++;
  4092.   while (1) {
  4093.     if (bp >= limit) {
  4094.       error_with_line (line_for_error (start_line),
  4095.                "unterminated string or character constant");
  4096.       if (eofp)
  4097.     *eofp = 1;
  4098.       break;
  4099.     }
  4100.     c = *bp++;
  4101.     if (c == '\\') {
  4102.       while (*bp == '\\' && bp[1] == '\n') {
  4103.     if (backslash_newlines_p)
  4104.       *backslash_newlines_p = 1;
  4105.     if (count_newlines)
  4106.       ++*count_newlines;
  4107.     bp += 2;
  4108.       }
  4109.       if (*bp == '\n' && count_newlines) {
  4110.     if (backslash_newlines_p)
  4111.       *backslash_newlines_p = 1;
  4112.     ++*count_newlines;
  4113.       }
  4114.       bp++;
  4115.     } else if (c == '\n') {
  4116.       if (traditional) {
  4117.      /* Unterminated strings and character constants are 'legal'.  */
  4118.      bp--;    /* Don't consume the newline. */
  4119.      if (eofp)
  4120.        *eofp = 1;
  4121.      break;
  4122.       }
  4123.       if (match == '\'') {
  4124.     error_with_line (line_for_error (start_line),
  4125.              "unterminated character constant");
  4126.     bp--;
  4127.     if (eofp)
  4128.       *eofp = 1;
  4129.     break;
  4130.       }
  4131.       if (traditional) {    /* Unterminated strings are 'legal'.  */
  4132.     if (eofp)
  4133.       *eofp = 1;
  4134.     break;
  4135.       }
  4136.       /* If not traditional, then allow newlines inside strings.  */
  4137.       if (count_newlines)
  4138.     ++*count_newlines;
  4139.     } else if (c == match)
  4140.       break;
  4141.   }
  4142.   return bp;
  4143. }
  4144.  
  4145. /*
  4146.  * write out a #line command, for instance, after an #include file.
  4147.  * If CONDITIONAL is nonzero, we can omit the #line if it would
  4148.  * appear to be a no-op, and we can output a few newlines instead
  4149.  * if we want to increase the line number by a small amount.
  4150.  * FILE_CHANGE says whether we are entering a file, leaving, or neither.
  4151.  */
  4152.  
  4153. void
  4154. output_line_command (ip, op, conditional, file_change)
  4155.      FILE_BUF *ip, *op;
  4156.      int conditional;
  4157.      enum file_change_code file_change;
  4158. {
  4159.   int len;
  4160.   char line_cmd_buf[500];
  4161.  
  4162.   if (no_line_commands
  4163.       || ip->fname == NULL
  4164.       || no_output) {
  4165.     op->lineno = ip->lineno;
  4166.     return;
  4167.   }
  4168.  
  4169.   if (conditional) {
  4170.     if (ip->lineno == op->lineno)
  4171.       return;
  4172.  
  4173.     /* If the inherited line number is a little too small,
  4174.        output some newlines instead of a #line command.  */
  4175.     if (ip->lineno > op->lineno && ip->lineno < op->lineno + 8) {
  4176.       check_expand (op, 10);
  4177.       while (ip->lineno > op->lineno) {
  4178.     *op->bufp++ = '\n';
  4179.     op->lineno++;
  4180.       }
  4181.       return;
  4182.     }
  4183.   }
  4184.  
  4185. #ifdef OUTPUT_LINE_COMMANDS
  4186.   sprintf (line_cmd_buf, "#line %d \"%s\"", ip->lineno, ip->fname);
  4187. #else
  4188.   sprintf (line_cmd_buf, "# %d \"%s\"", ip->lineno, ip->fname);
  4189. #endif
  4190.   if (file_change != same_file)
  4191.     strcat (line_cmd_buf, file_change == enter_file ? " 1" : " 2");
  4192.   len = strlen (line_cmd_buf);
  4193.   line_cmd_buf[len++] = '\n';
  4194.   check_expand (op, len + 1);
  4195.   if (op->bufp > op->buf && op->bufp[-1] != '\n')
  4196.     *op->bufp++ = '\n';
  4197.   bcopy (line_cmd_buf, op->bufp, len);
  4198.   op->bufp += len;
  4199.   op->lineno = ip->lineno;
  4200. }
  4201.  
  4202. /* This structure represents one parsed argument in a macro call.
  4203.    `raw' points to the argument text as written (`raw_length' is its length).
  4204.    `expanded' points to the argument's macro-expansion
  4205.    (its length is `expand_length').
  4206.    `stringified_length' is the length the argument would have
  4207.    if stringified.
  4208.    `free1' and `free2', if nonzero, point to blocks to be freed
  4209.    when the macro argument data is no longer needed.  */
  4210.  
  4211. struct argdata {
  4212.   U_CHAR *raw, *expanded;
  4213.   int raw_length, expand_length;
  4214.   int stringified_length;
  4215.   U_CHAR *free1, *free2;
  4216.   char newlines;
  4217.   char comments;
  4218. };
  4219.  
  4220. /* Expand a macro call.
  4221.    HP points to the symbol that is the macro being called.
  4222.    Put the result of expansion onto the input stack
  4223.    so that subsequent input by our caller will use it.
  4224.  
  4225.    If macro wants arguments, caller has already verified that
  4226.    an argument list follows; arguments come from the input stack.  */
  4227.  
  4228. void
  4229. macroexpand (hp, op)
  4230.      HASHNODE *hp;
  4231.      FILE_BUF *op;
  4232. {
  4233.   int nargs;
  4234.   DEFINITION *defn = hp->value.defn;
  4235.   register U_CHAR *xbuf;
  4236.   int xbuf_len;
  4237.   int start_line = instack[indepth].lineno;
  4238.  
  4239.   CHECK_DEPTH (return;);
  4240.  
  4241.   /* it might not actually be a macro.  */
  4242.   if (hp->type != T_MACRO) {
  4243.     special_symbol (hp, op);
  4244.     return;
  4245.   }
  4246.  
  4247.   nargs = defn->nargs;
  4248.  
  4249.   if (nargs >= 0) {
  4250.     register int i;
  4251.     struct argdata *args;
  4252.     char *parse_error = 0;
  4253.  
  4254.     args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
  4255.  
  4256.     for (i = 0; i < nargs; i++) {
  4257.       args[i].raw = args[i].expanded = (U_CHAR *) "";
  4258.       args[i].raw_length = args[i].expand_length
  4259.     = args[i].stringified_length = 0;
  4260.       args[i].free1 = args[i].free2 = 0;
  4261.     }
  4262.  
  4263.     /* Parse all the macro args that are supplied.  I counts them.
  4264.        The first NARGS args are stored in ARGS.
  4265.        The rest are discarded.  */
  4266.     i = 0;
  4267.     do {
  4268.       /* Discard the open-parenthesis or comma before the next arg.  */
  4269.       ++instack[indepth].bufp;
  4270.       parse_error
  4271.     = macarg ((i < nargs || (nargs == 0 && i == 0)) ? &args[i] : 0);
  4272.       if (parse_error)
  4273.     {
  4274.       error_with_line (line_for_error (start_line), parse_error);
  4275.       break;
  4276.     }
  4277.       i++;
  4278.     } while (*instack[indepth].bufp != ')');
  4279.  
  4280.     /* If we got one arg but it was just whitespace, call that 0 args.  */
  4281.     if (i == 1) {
  4282.       register U_CHAR *bp = args[0].raw;
  4283.       register U_CHAR *lim = bp + args[0].raw_length;
  4284.       while (bp != lim && is_space[*bp]) bp++;
  4285.       if (bp == lim)
  4286.     i = 0;
  4287.     }
  4288.  
  4289.     if (nargs == 0 && i > 0)
  4290.       error ("arguments given to macro `%s'", hp->name);
  4291.     else if (i < nargs) {
  4292.       /* traditional C allows foo() if foo wants one argument.  */
  4293.       if (nargs == 1 && i == 0 && traditional)
  4294.     ;
  4295.       else if (i == 0)
  4296.     error ("no args to macro `%s'", hp->name);
  4297.       else if (i == 1)
  4298.     error ("only 1 arg to macro `%s'", hp->name);
  4299.       else
  4300.     error ("only %d args to macro `%s'", i, hp->name);
  4301.     } else if (i > nargs)
  4302.       error ("too many (%d) args to macro `%s'", i, hp->name);
  4303.  
  4304.     /* Swallow the closeparen.  */
  4305.     ++instack[indepth].bufp;
  4306.  
  4307.     /* If macro wants zero args, we parsed the arglist for checking only.
  4308.        Read directly from the macro definition.  */
  4309.     if (nargs == 0) {
  4310.       xbuf = defn->expansion;
  4311.       xbuf_len = defn->length;
  4312.     } else {
  4313.       register U_CHAR *exp = defn->expansion;
  4314.       register int offset;    /* offset in expansion,
  4315.                    copied a piece at a time */
  4316.       register int totlen;    /* total amount of exp buffer filled so far */
  4317.  
  4318.       register struct reflist *ap;
  4319.  
  4320.       /* Macro really takes args.  Compute the expansion of this call.  */
  4321.  
  4322.       /* Compute length in characters of the macro's expansion.  */
  4323.       xbuf_len = defn->length;
  4324.       for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  4325.     if (ap->stringify)
  4326.       xbuf_len += args[ap->argno].stringified_length;
  4327.     else if (ap->raw_before || ap->raw_after || traditional)
  4328.       xbuf_len += args[ap->argno].raw_length;
  4329.     else
  4330.       xbuf_len += args[ap->argno].expand_length;
  4331.       }
  4332.  
  4333.       xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
  4334.  
  4335.       /* Generate in XBUF the complete expansion
  4336.      with arguments substituted in.
  4337.      TOTLEN is the total size generated so far.
  4338.      OFFSET is the index in the definition
  4339.      of where we are copying from.  */
  4340.       offset = totlen = 0;
  4341.       for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  4342.     register struct argdata *arg = &args[ap->argno];
  4343.  
  4344.     for (i = 0; i < ap->nchars; i++)
  4345.       xbuf[totlen++] = exp[offset++];
  4346.  
  4347.     if (ap->stringify != 0) {
  4348.       int arglen = arg->raw_length;
  4349.       int escaped = 0;
  4350.       int in_string = 0;
  4351.       int c;
  4352.       i = 0;
  4353.       while (i < arglen
  4354.          && (c = arg->raw[i], is_space[c]))
  4355.         i++;
  4356.       while (i < arglen
  4357.          && (c = arg->raw[arglen - 1], is_space[c]))
  4358.         arglen--;
  4359.       if (!traditional)
  4360.         xbuf[totlen++] = '\"'; /* insert beginning quote */
  4361.       for (; i < arglen; i++) {
  4362.         c = arg->raw[i];
  4363.  
  4364.         /* Special markers Newline Space
  4365.            generate nothing for a stringified argument.  */
  4366.         if (c == '\n' && arg->raw[i+1] != '\n') {
  4367.           i++;
  4368.           continue;
  4369.         }
  4370.  
  4371.         /* Internal sequences of whitespace are replaced by one space.  */
  4372.         if (c == '\n' ? arg->raw[i+1] == '\n' : is_space[c]) {
  4373.           while (1) {
  4374.         /* Note that Newline Space does occur within whitespace
  4375.            sequences; consider it part of the sequence.  */
  4376.         if (c == '\n' && is_space[arg->raw[i+1]])
  4377.           i += 2;
  4378.         else if (c != '\n' && is_space[c])
  4379.           i++;
  4380.         else break;
  4381.         c = arg->raw[i];
  4382.           }
  4383.           i--;
  4384.           c = ' ';
  4385.         }
  4386.  
  4387.         if (escaped)
  4388.           escaped = 0;
  4389.         else {
  4390.           if (c == '\\')
  4391.         escaped = 1;
  4392.           if (in_string) {
  4393.         if (c == in_string)
  4394.           in_string = 0;
  4395.           } else if (c == '\"' || c == '\'')
  4396.         in_string = c;
  4397.         }
  4398.  
  4399.         /* Escape these chars */
  4400.         if (c == '\"' || (in_string && c == '\\'))
  4401.           xbuf[totlen++] = '\\';
  4402.         if (isprint (c))
  4403.           xbuf[totlen++] = c;
  4404.         else {
  4405.           sprintf ((char *) &xbuf[totlen], "\\%03o", (unsigned int) c);
  4406.           totlen += 4;
  4407.         }
  4408.       }
  4409.       if (!traditional)
  4410.         xbuf[totlen++] = '\"'; /* insert ending quote */
  4411.     } else if (ap->raw_before || ap->raw_after || traditional) {
  4412.       U_CHAR *p1 = arg->raw;
  4413.       U_CHAR *l1 = p1 + arg->raw_length;
  4414.  
  4415.       if (ap->raw_before) {
  4416.         while (p1 != l1 && is_space[*p1]) p1++;
  4417.         while (p1 != l1 && is_idchar[*p1])
  4418.           xbuf[totlen++] = *p1++;
  4419.         /* Delete any no-reexpansion marker that follows
  4420.            an identifier at the beginning of the argument
  4421.            if the argument is concatenated with what precedes it.  */
  4422.         if (p1[0] == '\n' && p1[1] == '-')
  4423.           p1 += 2;
  4424.       }
  4425.       if (ap->raw_after) {
  4426.         /* Arg is concatenated after: delete trailing whitespace,
  4427.            whitespace markers, and no-reexpansion markers.  */
  4428.         while (p1 != l1) {
  4429.           if (is_space[l1[-1]]) l1--;
  4430.           else if (l1[-1] == '-') {
  4431.         U_CHAR *p2 = l1 - 1;
  4432.         /* If a `-' is preceded by an odd number of newlines then it
  4433.            and the last newline are a no-reexpansion marker.  */
  4434.         while (p2 != p1 && p2[-1] == '\n') p2--;
  4435.         if ((l1 - 1 - p2) & 1) {
  4436.           l1 -= 2;
  4437.         }
  4438.         else break;
  4439.           }
  4440.           else break;
  4441.         }
  4442.       }
  4443.       bcopy (p1, xbuf + totlen, l1 - p1);
  4444.       totlen += l1 - p1;
  4445.     } else {
  4446.       bcopy (arg->expanded, xbuf + totlen, arg->expand_length);
  4447.       totlen += arg->expand_length;
  4448.     }
  4449.  
  4450.     if (totlen > xbuf_len)
  4451.       abort ();
  4452.       }
  4453.  
  4454.       /* if there is anything left of the definition
  4455.      after handling the arg list, copy that in too. */
  4456.  
  4457.       for (i = offset; i < defn->length; i++)
  4458.     xbuf[totlen++] = exp[i];
  4459.  
  4460.       xbuf[totlen] = 0;
  4461.       xbuf_len = totlen;
  4462.  
  4463.       for (i = 0; i < nargs; i++) {
  4464.     if (args[i].free1 != 0)
  4465.       free (args[i].free1);
  4466.     if (args[i].free2 != 0)
  4467.       free (args[i].free2);
  4468.       }
  4469.     }
  4470.   } else {
  4471.     xbuf = defn->expansion;
  4472.     xbuf_len = defn->length;
  4473.   }
  4474.  
  4475.   /* Now put the expansion on the input stack
  4476.      so our caller will commence reading from it.  */
  4477.   {
  4478.     register FILE_BUF *ip2;
  4479.  
  4480.     ip2 = &instack[++indepth];
  4481.  
  4482.     ip2->fname = 0;
  4483.     ip2->lineno = 0;
  4484.     ip2->buf = xbuf;
  4485.     ip2->length = xbuf_len;
  4486.     ip2->bufp = xbuf;
  4487.     ip2->free_ptr = (nargs > 0) ? xbuf : 0;
  4488.     ip2->macro = hp;
  4489.     ip2->if_stack = if_stack;
  4490.  
  4491.     /* Recursive macro use sometimes works traditionally.
  4492.        #define foo(x,y) bar(x(y,0), y)
  4493.        foo(foo, baz)  */
  4494.  
  4495.     if (!traditional)
  4496.       hp->type = T_DISABLED;
  4497.   }
  4498. }
  4499.  
  4500. /*
  4501.  * Parse a macro argument and store the info on it into *ARGPTR.
  4502.  * Return nonzero to indicate a syntax error.
  4503.  */
  4504.  
  4505. char *
  4506. macarg (argptr)
  4507.      register struct argdata *argptr;
  4508. {
  4509.   FILE_BUF *ip = &instack[indepth];
  4510.   int paren = 0;
  4511.   int newlines = 0;
  4512.   int comments = 0;
  4513.  
  4514.   /* Try to parse as much of the argument as exists at this
  4515.      input stack level.  */
  4516.   U_CHAR *bp = macarg1 (ip->bufp, ip->buf + ip->length,
  4517.             &paren, &newlines, &comments);
  4518.  
  4519.   /* If we find the end of the argument at this level,
  4520.      set up *ARGPTR to point at it in the input stack.  */
  4521.   if (!(ip->fname != 0 && (newlines != 0 || comments != 0))
  4522.       && bp != ip->buf + ip->length) {
  4523.     if (argptr != 0) {
  4524.       argptr->raw = ip->bufp;
  4525.       argptr->raw_length = bp - ip->bufp;
  4526.     }
  4527.     ip->bufp = bp;
  4528.   } else {
  4529.     /* This input stack level ends before the macro argument does.
  4530.        We must pop levels and keep parsing.
  4531.        Therefore, we must allocate a temporary buffer and copy
  4532.        the macro argument into it.  */
  4533.     int bufsize = bp - ip->bufp;
  4534.     int extra = newlines;
  4535.     U_CHAR *buffer = (U_CHAR *) xmalloc (bufsize + extra + 1);
  4536.     int final_start = 0;
  4537.  
  4538.     bcopy (ip->bufp, buffer, bufsize);
  4539.     ip->bufp = bp;
  4540.     ip->lineno += newlines;
  4541.  
  4542.     while (bp == ip->buf + ip->length) {
  4543.       if (instack[indepth].macro == 0) {
  4544.     free (buffer);
  4545.     return "unterminated macro call";
  4546.       }
  4547.       ip->macro->type = T_MACRO;
  4548.       if (ip->free_ptr)
  4549.     free (ip->free_ptr);
  4550.       ip = &instack[--indepth];
  4551.       newlines = 0;
  4552.       comments = 0;
  4553.       bp = macarg1 (ip->bufp, ip->buf + ip->length, &paren,
  4554.             &newlines, &comments);
  4555.       final_start = bufsize;
  4556.       bufsize += bp - ip->bufp;
  4557.       extra += newlines;
  4558.       buffer = (U_CHAR *) xrealloc (buffer, bufsize + extra + 1);
  4559.       bcopy (ip->bufp, buffer + bufsize - (bp - ip->bufp), bp - ip->bufp);
  4560.       ip->bufp = bp;
  4561.       ip->lineno += newlines;
  4562.     }
  4563.  
  4564.     /* Now, if arg is actually wanted, record its raw form,
  4565.        discarding comments and duplicating newlines in whatever
  4566.        part of it did not come from a macro expansion.
  4567.        EXTRA space has been preallocated for duplicating the newlines.
  4568.        FINAL_START is the index of the start of that part.  */
  4569.     if (argptr != 0) {
  4570.       argptr->raw = buffer;
  4571.       argptr->raw_length = bufsize;
  4572.       argptr->free1 = buffer;
  4573.       argptr->newlines = newlines;
  4574.       argptr->comments = comments;
  4575.       if ((newlines || comments) && ip->fname != 0)
  4576.     argptr->raw_length
  4577.       = final_start +
  4578.         discard_comments (argptr->raw + final_start,
  4579.                   argptr->raw_length - final_start,
  4580.                   newlines);
  4581.       argptr->raw[argptr->raw_length] = 0;
  4582.       if (argptr->raw_length > bufsize + extra)
  4583.     abort ();
  4584.     }
  4585.   }
  4586.  
  4587.   /* If we are not discarding this argument,
  4588.      macroexpand it and compute its length as stringified.
  4589.      All this info goes into *ARGPTR.  */
  4590.  
  4591.   if (argptr != 0) {
  4592.     FILE_BUF obuf;
  4593.     register U_CHAR *buf, *lim;
  4594.     register int totlen;
  4595.  
  4596.     obuf = expand_to_temp_buffer (argptr->raw,
  4597.                   argptr->raw + argptr->raw_length,
  4598.                   1);
  4599.  
  4600.     argptr->expanded = obuf.buf;
  4601.     argptr->expand_length = obuf.length;
  4602.     argptr->free2 = obuf.buf;
  4603.  
  4604.     buf = argptr->raw;
  4605.     lim = buf + argptr->raw_length;
  4606.  
  4607.     /* If ANSI, discard leading and trailing space.  */
  4608.     if (!traditional) {
  4609.       while (buf != lim && is_space[*buf])
  4610.     buf++;
  4611.       while (buf != lim && is_space[lim[-1]])
  4612.     lim--;
  4613.     }
  4614.     totlen = traditional ? 0 : 2;    /* Count opening and closing quote.  */
  4615.     while (buf != lim) {
  4616.       register U_CHAR c = *buf++;
  4617.       totlen++;
  4618.       /* If ANSI, replace internal sequences of whitespace with one space.  */
  4619.       if (is_space[c] && !traditional)
  4620.     SKIP_ALL_WHITE_SPACE (buf);
  4621.       else if (c == '\"' || c == '\\') /* escape these chars */
  4622.     totlen++;
  4623.       else if (!isprint (c))
  4624.     totlen += 3;
  4625.     }
  4626.     argptr->stringified_length = totlen;
  4627.   }
  4628.   return 0;
  4629. }
  4630.  
  4631. /* Scan text from START (inclusive) up to LIMIT (exclusive),
  4632.    counting parens in *DEPTHPTR,
  4633.    and return if reach LIMIT
  4634.    or before a `)' that would make *DEPTHPTR negative
  4635.    or before a comma when *DEPTHPTR is zero.
  4636.    Single and double quotes are matched and termination
  4637.    is inhibited within them.  Comments also inhibit it.
  4638.    Value returned is pointer to stopping place.
  4639.  
  4640.    Increment *NEWLINES each time a newline is passed.
  4641.    Set *COMMENTS to 1 if a comment is seen.  */
  4642.  
  4643. U_CHAR *
  4644. macarg1 (start, limit, depthptr, newlines, comments)
  4645.      U_CHAR *start;
  4646.      register U_CHAR *limit;
  4647.      int *depthptr, *newlines, *comments;
  4648. {
  4649.   register U_CHAR *bp = start;
  4650.  
  4651.   while (bp < limit) {
  4652.     switch (*bp) {
  4653.     case '(':
  4654.       (*depthptr)++;
  4655.       break;
  4656.     case ')':
  4657.       if (--(*depthptr) < 0)
  4658.     return bp;
  4659.       break;
  4660.     case '\\':
  4661.       /* Traditionally, backslash makes following char not special.  */
  4662.       if (!traditional)
  4663.     break;
  4664.       if (bp + 1 < limit)
  4665.     {
  4666.       bp++;
  4667.       /* But count source lines anyway.  */
  4668.       if (*bp == '\n')
  4669.         ++*newlines;
  4670.     }
  4671.       break;
  4672.     case '\n':
  4673.       ++*newlines;
  4674.       break;
  4675.     case '/':
  4676.       if (bp[1] == '\\' && bp[2] == '\n')
  4677.     newline_fix (bp + 1);
  4678.       if (cplusplus && bp[1] == '/') {
  4679.     *comments = 1;
  4680.     bp += 2;
  4681.     while (bp < limit && *bp++ != '\n') ;
  4682.     ++*newlines;
  4683.     break;
  4684.       }
  4685.       if (bp[1] != '*' || bp + 1 >= limit)
  4686.     break;
  4687.       *comments = 1;
  4688.       bp += 2;
  4689.       while (bp + 1 < limit) {
  4690.     if (bp[0] == '*'
  4691.         && bp[1] == '\\' && bp[2] == '\n')
  4692.       newline_fix (bp + 1);
  4693.     if (bp[0] == '*' && bp[1] == '/')
  4694.       break;
  4695.     if (*bp == '\n') ++*newlines;
  4696.     bp++;
  4697.       }
  4698.       bp += 1;
  4699.       break;
  4700.     case '\'':
  4701.     case '\"':
  4702.       {
  4703.     int quotec;
  4704.     for (quotec = *bp++; bp + 1 < limit && *bp != quotec; bp++) {
  4705.       if (*bp == '\\') {
  4706.         bp++;
  4707.         if (*bp == '\n')
  4708.           ++*newlines;
  4709.         while (*bp == '\\' && bp[1] == '\n') {
  4710.           bp += 2;
  4711.         }
  4712.       } else if (*bp == '\n') {
  4713.         ++*newlines;
  4714.         if (quotec == '\'')
  4715.           break;
  4716.       }
  4717.     }
  4718.       }
  4719.       break;
  4720.     case ',':
  4721.       if ((*depthptr) == 0)
  4722.     return bp;
  4723.       break;
  4724.     }
  4725.     bp++;
  4726.   }
  4727.  
  4728.   return bp;
  4729. }
  4730.  
  4731. /* Discard comments and duplicate newlines
  4732.    in the string of length LENGTH at START,
  4733.    except inside of string constants.
  4734.    The string is copied into itself with its beginning staying fixed.  
  4735.  
  4736.    NEWLINES is the number of newlines that must be duplicated.
  4737.    We assume that that much extra space is available past the end
  4738.    of the string.  */
  4739.  
  4740. int
  4741. discard_comments (start, length, newlines)
  4742.      U_CHAR *start;
  4743.      int length;
  4744.      int newlines;
  4745. {
  4746.   register U_CHAR *ibp;
  4747.   register U_CHAR *obp;
  4748.   register U_CHAR *limit;
  4749.   register int c;
  4750.  
  4751.   /* If we have newlines to duplicate, copy everything
  4752.      that many characters up.  Then, in the second part,
  4753.      we will have room to insert the newlines
  4754.      while copying down.
  4755.      NEWLINES may actually be too large, because it counts
  4756.      newlines in string constants, and we don't duplicate those.
  4757.      But that does no harm.  */
  4758.   if (newlines > 0) {
  4759.     ibp = start + length;
  4760.     obp = ibp + newlines;
  4761.     limit = start;
  4762.     while (limit != ibp)
  4763.       *--obp = *--ibp;
  4764.   }
  4765.  
  4766.   ibp = start + newlines;
  4767.   limit = start + length + newlines;
  4768.   obp = start;
  4769.  
  4770.   while (ibp < limit) {
  4771.     *obp++ = c = *ibp++;
  4772.     switch (c) {
  4773.     case '\n':
  4774.       /* Duplicate the newline.  */
  4775.       *obp++ = '\n';
  4776.       break;
  4777.  
  4778.     case '\\':
  4779.       if (*ibp == '\n') {
  4780.     obp--;
  4781.     ibp++;
  4782.       }
  4783.       break;
  4784.  
  4785.     case '/':
  4786.       if (*ibp == '\\' && ibp[1] == '\n')
  4787.     newline_fix (ibp);
  4788.       /* Delete any comment.  */
  4789.       if (cplusplus && ibp[0] == '/') {
  4790.     obp--;
  4791.     ibp++;
  4792.     while (ibp < limit && *ibp++ != '\n') ;
  4793.     break;
  4794.       }
  4795.       if (ibp[0] != '*' || ibp + 1 >= limit)
  4796.     break;
  4797.       obp--;
  4798.       ibp++;
  4799.       while (ibp + 1 < limit) {
  4800.     if (ibp[0] == '*'
  4801.         && ibp[1] == '\\' && ibp[2] == '\n')
  4802.       newline_fix (ibp + 1);
  4803.     if (ibp[0] == '*' && ibp[1] == '/')
  4804.       break;
  4805.     ibp++;
  4806.       }
  4807.       ibp += 2;
  4808.       break;
  4809.  
  4810.     case '\'':
  4811.     case '\"':
  4812.       /* Notice and skip strings, so that we don't
  4813.      think that comments start inside them,
  4814.      and so we don't duplicate newlines in them.  */
  4815.       {
  4816.     int quotec = c;
  4817.     while (ibp < limit) {
  4818.       *obp++ = c = *ibp++;
  4819.       if (c == quotec)
  4820.         break;
  4821.       if (c == '\n' && quotec == '\'')
  4822.         break;
  4823.       if (c == '\\' && ibp < limit) {
  4824.         while (*ibp == '\\' && ibp[1] == '\n')
  4825.           ibp += 2;
  4826.         *obp++ = *ibp++;
  4827.       }
  4828.     }
  4829.       }
  4830.       break;
  4831.     }
  4832.   }
  4833.  
  4834.   return obp - start;
  4835. }
  4836.  
  4837. /*
  4838.  * error - print error message and increment count of errors.
  4839.  */
  4840. error (msg, arg1, arg2, arg3)
  4841.      char *msg;
  4842. {
  4843.   int i;
  4844.   FILE_BUF *ip = NULL;
  4845.  
  4846.   for (i = indepth; i >= 0; i--)
  4847.     if (instack[i].fname != NULL) {
  4848.       ip = &instack[i];
  4849.       break;
  4850.     }
  4851.  
  4852.   if (ip != NULL)
  4853.     fprintf (stderr, "%s:%d: ", ip->fname, ip->lineno);
  4854.   fprintf (stderr, msg, arg1, arg2, arg3);
  4855.   fprintf (stderr, "\n");
  4856.   errors++;
  4857.   return 0;
  4858. }
  4859.  
  4860. /* Error including a message from `errno'.  */
  4861.  
  4862. error_from_errno (name)
  4863.      char *name;
  4864. {
  4865.   int i;
  4866.   FILE_BUF *ip = NULL;
  4867.   extern int errno, sys_nerr;
  4868.  
  4869.   for (i = indepth; i >= 0; i--)
  4870.     if (instack[i].fname != NULL) {
  4871.       ip = &instack[i];
  4872.       break;
  4873.     }
  4874.  
  4875.   if (ip != NULL)
  4876.     fprintf (stderr, "%s:%d: ", ip->fname, ip->lineno);
  4877.  
  4878.   if (errno < sys_nerr)
  4879.     fprintf (stderr, "%s: %s\n", name, strerror (errno));
  4880.   else
  4881.     fprintf (stderr, "%s: undocumented I/O error\n", name);
  4882.  
  4883.   errors++;
  4884.   return 0;
  4885. }
  4886.  
  4887. /* Print error message but don't count it.  */
  4888.  
  4889. warning (msg, arg1, arg2, arg3)
  4890.      char *msg;
  4891. {
  4892.   int i;
  4893.   FILE_BUF *ip = NULL;
  4894.  
  4895.   if (inhibit_warnings)
  4896.     return 0;
  4897.  
  4898.   for (i = indepth; i >= 0; i--)
  4899.     if (instack[i].fname != NULL) {
  4900.       ip = &instack[i];
  4901.       break;
  4902.     }
  4903.  
  4904.   if (ip != NULL)
  4905.     fprintf (stderr, "%s:%d: ", ip->fname, ip->lineno);
  4906.   fprintf (stderr, "warning: ");
  4907.   fprintf (stderr, msg, arg1, arg2, arg3);
  4908.   fprintf (stderr, "\n");
  4909.   return 0;
  4910. }
  4911.  
  4912. error_with_line (line, msg, arg1, arg2, arg3)
  4913.      int line;
  4914.      char *msg;
  4915. {
  4916.   int i;
  4917.   FILE_BUF *ip = NULL;
  4918.  
  4919.   for (i = indepth; i >= 0; i--)
  4920.     if (instack[i].fname != NULL) {
  4921.       ip = &instack[i];
  4922.       break;
  4923.     }
  4924.  
  4925.   if (ip != NULL)
  4926.     fprintf (stderr, "%s:%d: ", ip->fname, line);
  4927.   fprintf (stderr, msg, arg1, arg2, arg3);
  4928.   fprintf (stderr, "\n");
  4929.   errors++;
  4930.   return 0;
  4931. }
  4932.  
  4933. /* Return the line at which an error occurred.
  4934.    The error is not necessarily associated with the current spot
  4935.    in the input stack, so LINE says where.  LINE will have been
  4936.    copied from ip->lineno for the current input level.
  4937.    If the current level is for a file, we return LINE.
  4938.    But if the current level is not for a file, LINE is meaningless.
  4939.    In that case, we return the lineno of the innermost file.  */
  4940. int
  4941. line_for_error (line)
  4942.      int line;
  4943. {
  4944.   int i;
  4945.   int line1 = line;
  4946.  
  4947.   for (i = indepth; i >= 0; ) {
  4948.     if (instack[i].fname != 0)
  4949.       return line1;
  4950.     i--;
  4951.     if (i < 0)
  4952.       return 0;
  4953.     line1 = instack[i].lineno;
  4954.   }
  4955. }
  4956.  
  4957. /*
  4958.  * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
  4959.  *
  4960.  * As things stand, nothing is ever placed in the output buffer to be
  4961.  * removed again except when it's KNOWN to be part of an identifier,
  4962.  * so flushing and moving down everything left, instead of expanding,
  4963.  * should work ok.
  4964.  */
  4965.  
  4966. int
  4967. grow_outbuf (obuf, needed)
  4968.      register FILE_BUF *obuf;
  4969.      register int needed;
  4970. {
  4971.   register U_CHAR *p;
  4972.   int minsize;
  4973.  
  4974.   if (obuf->length - (obuf->bufp - obuf->buf) > needed)
  4975.     return;
  4976.  
  4977.   /* Make it at least twice as big as it is now.  */
  4978.   obuf->length *= 2;
  4979.   /* Make it have at least 150% of the free space we will need.  */
  4980.   minsize = (3 * needed) / 2 + (obuf->bufp - obuf->buf);
  4981.   if (minsize > obuf->length)
  4982.     obuf->length = minsize;
  4983.  
  4984.   if ((p = (U_CHAR *) xrealloc (obuf->buf, obuf->length)) == NULL)
  4985.     memory_full ();
  4986.  
  4987.   obuf->bufp = p + (obuf->bufp - obuf->buf);
  4988.   obuf->buf = p;
  4989. }
  4990.  
  4991. /* Symbol table for macro names and special symbols */
  4992.  
  4993. /*
  4994.  * install a name in the main hash table, even if it is already there.
  4995.  *   name stops with first non alphanumeric, except leading '#'.
  4996.  * caller must check against redefinition if that is desired.
  4997.  * delete_macro () removes things installed by install () in fifo order.
  4998.  * this is important because of the `defined' special symbol used
  4999.  * in #if, and also if pushdef/popdef directives are ever implemented.
  5000.  *
  5001.  * If LEN is >= 0, it is the length of the name.
  5002.  * Otherwise, compute the length by scanning the entire name.
  5003.  *
  5004.  * If HASH is >= 0, it is the precomputed hash code.
  5005.  * Otherwise, compute the hash code.
  5006.  */
  5007. HASHNODE *
  5008. install (name, len, type, value, hash)
  5009.      U_CHAR *name;
  5010.      int len;
  5011.      enum node_type type;
  5012.      int value;
  5013.      int hash;
  5014.         /* watch out here if sizeof (U_CHAR *) != sizeof (int) */
  5015. {
  5016.   register HASHNODE *hp;
  5017.   register int i, bucket;
  5018.   register U_CHAR *p, *q;
  5019.  
  5020.   if (len < 0) {
  5021.     p = name;
  5022.     while (is_idchar[*p])
  5023.       p++;
  5024.     len = p - name;
  5025.   }
  5026.  
  5027.   if (hash < 0)
  5028.     hash = hashf (name, len, HASHSIZE);
  5029.  
  5030.   i = sizeof (HASHNODE) + len + 1;
  5031.   hp = (HASHNODE *) xmalloc (i);
  5032.   bucket = hash;
  5033.   hp->bucket_hdr = &hashtab[bucket];
  5034.   hp->next = hashtab[bucket];
  5035.   hashtab[bucket] = hp;
  5036.   hp->prev = NULL;
  5037.   if (hp->next != NULL)
  5038.     hp->next->prev = hp;
  5039.   hp->type = type;
  5040.   hp->length = len;
  5041.   hp->value.ival = value;
  5042.   hp->name = ((U_CHAR *) hp) + sizeof (HASHNODE);
  5043.   p = hp->name;
  5044.   q = name;
  5045.   for (i = 0; i < len; i++)
  5046.     *p++ = *q++;
  5047.   hp->name[len] = 0;
  5048.   return hp;
  5049. }
  5050.  
  5051. /*
  5052.  * find the most recent hash node for name name (ending with first
  5053.  * non-identifier char) installed by install
  5054.  *
  5055.  * If LEN is >= 0, it is the length of the name.
  5056.  * Otherwise, compute the length by scanning the entire name.
  5057.  *
  5058.  * If HASH is >= 0, it is the precomputed hash code.
  5059.  * Otherwise, compute the hash code.
  5060.  */
  5061. HASHNODE *
  5062. lookup (name, len, hash)
  5063.      U_CHAR *name;
  5064.      int len;
  5065.      int hash;
  5066. {
  5067.   register U_CHAR *bp;
  5068.   register HASHNODE *bucket;
  5069.  
  5070.   if (len < 0) {
  5071.     for (bp = name; is_idchar[*bp]; bp++) ;
  5072.     len = bp - name;
  5073.   }
  5074.  
  5075.   if (hash < 0)
  5076.     hash = hashf (name, len, HASHSIZE);
  5077.  
  5078.   bucket = hashtab[hash];
  5079.   while (bucket) {
  5080.     if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
  5081.       return bucket;
  5082.     bucket = bucket->next;
  5083.   }
  5084.   return NULL;
  5085. }
  5086.  
  5087. /*
  5088.  * Delete a hash node.  Some weirdness to free junk from macros.
  5089.  * More such weirdness will have to be added if you define more hash
  5090.  * types that need it.
  5091.  */
  5092.  
  5093. /* Note that the DEFINITION of a macro is removed from the hash table
  5094.    but its storage is not freed.  This would be a storage leak
  5095.    except that it is not reasonable to keep undefining and redefining
  5096.    large numbers of macros many times.
  5097.    In any case, this is necessary, because a macro can be #undef'd
  5098.    in the middle of reading the arguments to a call to it.
  5099.    If #undef freed the DEFINITION, that would crash.  */
  5100.  
  5101. delete_macro (hp)
  5102.      HASHNODE *hp;
  5103. {
  5104.  
  5105.   if (hp->prev != NULL)
  5106.     hp->prev->next = hp->next;
  5107.   if (hp->next != NULL)
  5108.     hp->next->prev = hp->prev;
  5109.  
  5110.   /* make sure that the bucket chain header that
  5111.      the deleted guy was on points to the right thing afterwards. */
  5112.   if (hp == *hp->bucket_hdr)
  5113.     *hp->bucket_hdr = hp->next;
  5114.  
  5115. #if 0
  5116.   if (hp->type == T_MACRO) {
  5117.     DEFINITION *d = hp->value.defn;
  5118.     struct reflist *ap, *nextap;
  5119.  
  5120.     for (ap = d->pattern; ap != NULL; ap = nextap) {
  5121.       nextap = ap->next;
  5122.       free (ap);
  5123.     }
  5124.     free (d);
  5125.   }
  5126. #endif
  5127.   free (hp);
  5128. }
  5129.  
  5130. /*
  5131.  * return hash function on name.  must be compatible with the one
  5132.  * computed a step at a time, elsewhere
  5133.  */
  5134. int
  5135. hashf (name, len, hashsize)
  5136.      register U_CHAR *name;
  5137.      register int len;
  5138.      int hashsize;
  5139. {
  5140.   register int r = 0;
  5141.  
  5142.   while (len--)
  5143.     r = HASHSTEP (r, *name++);
  5144.  
  5145.   return MAKE_POS (r) % hashsize;
  5146. }
  5147.  
  5148. /* Dump all macro definitions as #defines to stdout.  */
  5149.  
  5150. void
  5151. dump_all_macros ()
  5152. {
  5153.   int bucket;
  5154.  
  5155.   for (bucket = 0; bucket < HASHSIZE; bucket++) {
  5156.     register HASHNODE *hp;
  5157.  
  5158.     for (hp = hashtab[bucket]; hp; hp= hp->next) {
  5159.       if (hp->type == T_MACRO) {
  5160.     register DEFINITION *defn = hp->value.defn;
  5161.     struct reflist *ap;
  5162.     int offset;
  5163.     int concat;
  5164.  
  5165.  
  5166.     /* Print the definition of the macro HP.  */
  5167.  
  5168.     printf ("#define %s", hp->name);
  5169.     if (defn->nargs >= 0) {
  5170.       int i;
  5171.  
  5172.       printf ("(");
  5173.       for (i = 0; i < defn->nargs; i++) {
  5174.         dump_arg_n (defn, i);
  5175.         if (i + 1 < defn->nargs)
  5176.           printf (", ");
  5177.       }
  5178.       printf (")");
  5179.     }
  5180.  
  5181.     printf (" ");
  5182.  
  5183.     offset = 0;
  5184.     concat = 0;
  5185.     for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  5186.       dump_defn_1 (defn->expansion, offset, ap->nchars);
  5187.       if (ap->nchars != 0)
  5188.         concat = 0;
  5189.       offset += ap->nchars;
  5190.       if (ap->stringify)
  5191.         printf (" #");
  5192.       if (ap->raw_before && !concat)
  5193.         printf (" ## ");
  5194.       concat = 0;
  5195.       dump_arg_n (defn, ap->argno);
  5196.       if (ap->raw_after) {
  5197.         printf (" ## ");
  5198.         concat = 1;
  5199.       }
  5200.     }
  5201.     dump_defn_1 (defn->expansion, offset, defn->length - offset);
  5202.     printf ("\n");
  5203.       }
  5204.     }
  5205.   }
  5206. }
  5207.  
  5208. /* Output to stdout a substring of a macro definition.
  5209.    BASE is the beginning of the definition.
  5210.    Output characters START thru LENGTH.
  5211.    Discard newlines outside of strings, thus
  5212.    converting funny-space markers to ordinary spaces.  */
  5213.  
  5214. dump_defn_1 (base, start, length)
  5215.      U_CHAR *base;
  5216.      int start;
  5217.      int length;
  5218. {
  5219.   U_CHAR *p = base + start;
  5220.   U_CHAR *limit = base + start + length;
  5221.  
  5222.   while (p < limit) {
  5223.     if (*p != '\n')
  5224.       putchar (*p);
  5225.     else if (*p == '\"' || *p =='\'') {
  5226.       U_CHAR *p1 = skip_quoted_string (p, limit, 0, 0, 0, 0);
  5227.       fwrite (p, p1 - p, 1, stdout);
  5228.       p = p1 - 1;
  5229.     }
  5230.     p++;
  5231.   }
  5232. }
  5233.  
  5234. /* Print the name of argument number ARGNUM of macro definition DEFN.
  5235.    Recall that DEFN->argnames contains all the arg names
  5236.    concatenated in reverse order with comma-space in between.  */
  5237.  
  5238. dump_arg_n (defn, argnum)
  5239.      DEFINITION *defn;
  5240.      int argnum;
  5241. {
  5242.   register U_CHAR *p = defn->argnames;
  5243.   while (argnum + 1 < defn->nargs) {
  5244.     p = (U_CHAR *) index (p, ' ') + 1;
  5245.     argnum++;
  5246.   }
  5247.  
  5248.   while (*p && *p != ',') {
  5249.     putchar (*p);
  5250.     p++;
  5251.   }
  5252. }
  5253.  
  5254. /* Initialize syntactic classifications of characters.  */
  5255.  
  5256. initialize_char_syntax ()
  5257. {
  5258.   register int i;
  5259.  
  5260.   /*
  5261.    * Set up is_idchar and is_idstart tables.  These should be
  5262.    * faster than saying (is_alpha (c) || c == '_'), etc.
  5263.    * Must do set up these things before calling any routines tthat
  5264.    * refer to them.
  5265.    */
  5266.   for (i = 'a'; i <= 'z'; i++) {
  5267.     is_idchar[i - 'a' + 'A'] = 1;
  5268.     is_idchar[i] = 1;
  5269.     is_idstart[i - 'a' + 'A'] = 1;
  5270.     is_idstart[i] = 1;
  5271.   }
  5272.   for (i = '0'; i <= '9'; i++)
  5273.     is_idchar[i] = 1;
  5274.   is_idchar['_'] = 1;
  5275.   is_idstart['_'] = 1;
  5276.   is_idchar['$'] = dollars_in_ident;
  5277.   is_idstart['$'] = dollars_in_ident;
  5278.  
  5279.   /* horizontal space table */
  5280.   is_hor_space[' '] = 1;
  5281.   is_hor_space['\t'] = 1;
  5282.   is_hor_space['\v'] = 1;
  5283.   is_hor_space['\f'] = 1;
  5284.   is_hor_space['\r'] = 1;
  5285.  
  5286.   is_space[' '] = 1;
  5287.   is_space['\t'] = 1;
  5288.   is_space['\v'] = 1;
  5289.   is_space['\f'] = 1;
  5290.   is_space['\n'] = 1;
  5291.   is_space['\r'] = 1;
  5292. }
  5293.  
  5294. /* Initialize the built-in macros.  */
  5295.  
  5296. initialize_builtins ()
  5297. {
  5298.   install ("__LINE__", -1, T_SPECLINE, 0, -1);
  5299.   install ("__DATE__", -1, T_DATE, 0, -1);
  5300.   install ("__FILE__", -1, T_FILE, 0, -1);
  5301.   install ("__BASE_FILE__", -1, T_BASE_FILE, 0, -1);
  5302.   install ("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, -1);
  5303.   install ("__VERSION__", -1, T_VERSION, 0, -1);
  5304.   install ("__TIME__", -1, T_TIME, 0, -1);
  5305.   if (!traditional)
  5306.     install ("__STDC__", -1, T_CONST, STDC_VALUE, -1);
  5307. /*  install ("__GNU__", -1, T_CONST, 1, -1);  */
  5308. /*  This is supplied using a -D by the compiler driver
  5309.     so that it is present only when truly compiling with GNU C.  */
  5310. }
  5311.  
  5312. /*
  5313.  * process a given definition string, for initialization
  5314.  * If STR is just an identifier, define it with value 1.
  5315.  * If STR has anything after the identifier, then it should
  5316.  * be identifier-space-definition.
  5317.  */
  5318. make_definition (str)
  5319.      U_CHAR *str;
  5320. {
  5321.   FILE_BUF *ip;
  5322.   struct directive *kt;
  5323.   U_CHAR *buf, *p;
  5324.  
  5325.   buf = str;
  5326.   p = str;
  5327.   while (is_idchar[*p]) p++;
  5328.   if (p == str) {
  5329.     error ("malformed option `-D %s'", str);
  5330.     return;
  5331.   }
  5332.   if (*p == 0) {
  5333.     buf = (U_CHAR *) alloca (p - buf + 4);
  5334.     strcpy ((char *)buf, str);
  5335.     strcat ((char *)buf, " 1");
  5336.   } else if (*p != ' ') {
  5337.     error ("malformed option `-D %s'", str);
  5338.     return;
  5339.   } else {
  5340.     U_CHAR *q;
  5341.     /* Copy the entire option so we can modify it.  */
  5342.     buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
  5343.     strncpy (buf, str, p - str);
  5344.     /* Change the = to a space.  */
  5345.     buf[p - str] = ' ';
  5346.     /* Scan for any backslash-newline and remove it.  */
  5347.     p++;
  5348.     q = &buf[p - str];
  5349.     while (*p) {
  5350.       if (*p == '\\' && p[1] == '\n')
  5351.     p += 2;
  5352.       /* Change newline chars into newline-markers.  */
  5353.       else if (*p == '\n')
  5354.     {
  5355.       *q++ = '\n';
  5356.       *q++ = '\n';
  5357.       p++;
  5358.     }
  5359.       else
  5360.     *q++ = *p++;
  5361.     }
  5362.     *q = 0;
  5363.   }
  5364.   
  5365.   ip = &instack[++indepth];
  5366.   ip->fname = "*Initialization*";
  5367.  
  5368.   ip->buf = ip->bufp = buf;
  5369.   ip->length = strlen (buf);
  5370.   ip->lineno = 1;
  5371.   ip->macro = 0;
  5372.   ip->free_ptr = 0;
  5373.   ip->if_stack = if_stack;
  5374.  
  5375.   for (kt = directive_table; kt->type != T_DEFINE; kt++)
  5376.     ;
  5377.  
  5378.   /* pass NULL as output ptr to do_define since we KNOW it never
  5379.      does any output.... */
  5380.   do_define (buf, buf + strlen (buf) , NULL, kt);
  5381.   --indepth;
  5382. }
  5383.  
  5384. /* JF, this does the work for the -U option */
  5385. make_undef (str)
  5386.      U_CHAR *str;
  5387. {
  5388.   FILE_BUF *ip;
  5389.   struct directive *kt;
  5390.  
  5391.   ip = &instack[++indepth];
  5392.   ip->fname = "*undef*";
  5393.  
  5394.   ip->buf = ip->bufp = str;
  5395.   ip->length = strlen (str);
  5396.   ip->lineno = 1;
  5397.   ip->macro = 0;
  5398.   ip->free_ptr = 0;
  5399.   ip->if_stack = if_stack;
  5400.  
  5401.   for (kt = directive_table; kt->type != T_UNDEF; kt++)
  5402.     ;
  5403.  
  5404.   do_undef (str,str + strlen (str) - 1, NULL, kt);
  5405.   --indepth;
  5406. }
  5407.  
  5408. /* Add output to `deps_buffer' for the -M switch.
  5409.    STRING points to the text to be output.
  5410.    SIZE is the number of bytes, or 0 meaning output until a null.
  5411.    If SIZE is nonzero, we break the line first, if it is long enough.  */
  5412.  
  5413. deps_output (string, size)
  5414.      char *string;
  5415.      int size;
  5416. {
  5417. #ifndef MAX_OUTPUT_COLUMNS
  5418. #define MAX_OUTPUT_COLUMNS 75
  5419. #endif
  5420.   if (size != 0 && deps_column != 0
  5421.       && size + deps_column > MAX_OUTPUT_COLUMNS) {
  5422.     deps_output ("\\\n  ", 0);
  5423.     deps_column = 0;
  5424.   }
  5425.  
  5426.   if (size == 0)
  5427.     size = strlen (string);
  5428.  
  5429.   if (deps_size + size + 1 > deps_allocated_size) {
  5430.     deps_allocated_size = deps_size + size + 50;
  5431.     deps_allocated_size *= 2;
  5432.     deps_buffer = (char *) xrealloc (deps_buffer, deps_allocated_size);
  5433.   }
  5434.   bcopy (string, &deps_buffer[deps_size], size);
  5435.   deps_size += size;
  5436.   deps_column += size;
  5437.   deps_buffer[deps_size] = 0;
  5438. }
  5439.  
  5440. #ifndef BSTRING
  5441.  
  5442. void
  5443. bzero (b, length)
  5444.      register char *b;
  5445.      register int length;
  5446. {
  5447. #ifdef VMS
  5448.   short zero = 0;
  5449.   long max_str = 65535;
  5450.  
  5451.   while (length > max_str) {
  5452.     (void) LIB$MOVC5 (&zero, &zero, &zero, &max_str, b);
  5453.     length -= max_str;
  5454.     b += max_str;
  5455.   }
  5456.   (void) LIB$MOVC5 (&zero, &zero, &zero, &length, b);
  5457. #else
  5458.   while (length-- > 0)
  5459.     *b++ = 0;
  5460. #endif /* not VMS */
  5461. }
  5462.  
  5463. void
  5464. bcopy (b1, b2, length)
  5465.      register char *b1;
  5466.      register char *b2;
  5467.      register int length;
  5468. {
  5469. #ifdef VMS
  5470.   long max_str = 65535;
  5471.  
  5472.   while (length > max_str) {
  5473.     (void) LIB$MOVC3 (&max_str, b1, b2);
  5474.     length -= max_str;
  5475.     b1 += max_str;
  5476.     b2 += max_str;
  5477.   }
  5478.   (void) LIB$MOVC3 (&length, b1, b2);
  5479. #else
  5480.   while (length-- > 0)
  5481.     *b2++ = *b1++;
  5482. #endif /* not VMS */
  5483. }
  5484.  
  5485. int
  5486. bcmp (b1, b2, length)    /* This could be a macro! */
  5487.      register char *b1;
  5488.      register char *b2;
  5489.       register int length;
  5490. {
  5491. #ifdef VMS
  5492.    struct dsc$descriptor_s src1 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b1};
  5493.    struct dsc$descriptor_s src2 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b2};
  5494.  
  5495.    return STR$COMPARE (&src1, &src2);
  5496. #else
  5497.    while (length-- > 0)
  5498.      if (*b1++ != *b2++)
  5499.        return 1;
  5500.  
  5501.    return 0;
  5502. #endif /* not VMS */
  5503. }
  5504. #endif /* not BSTRING */
  5505.  
  5506. void
  5507. fatal (str, arg)
  5508.      char *str, *arg;
  5509. {
  5510.   fprintf (stderr, "%s: ", progname);
  5511.   fprintf (stderr, str, arg);
  5512.   fprintf (stderr, "\n");
  5513.   exit (FATAL_EXIT_CODE);
  5514. }
  5515.  
  5516. /* More 'friendly' abort that prints the line and file.
  5517.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  5518.  
  5519. void
  5520. fancy_abort ()
  5521. {
  5522.   fatal ("Internal gcc abort.");
  5523. }
  5524.  
  5525. void
  5526. perror_with_name (name)
  5527.      char *name;
  5528. {
  5529.   extern int errno, sys_nerr;
  5530.  
  5531.   fprintf (stderr, "%s: ", progname);
  5532.   if (errno < sys_nerr)
  5533.     fprintf (stderr, "%s: %s\n", name, strerror (errno));
  5534.   else
  5535.     fprintf (stderr, "%s: undocumented I/O error\n", name);
  5536.   errors++;
  5537. }
  5538.  
  5539. void
  5540. pfatal_with_name (name)
  5541.      char *name;
  5542. {
  5543.   perror_with_name (name);
  5544. #ifdef VMS
  5545.   exit (vaxc$errno);
  5546. #else
  5547.   exit (FATAL_EXIT_CODE);
  5548. #endif
  5549. }
  5550.  
  5551.  
  5552. void
  5553. memory_full ()
  5554. {
  5555.   fatal ("Memory exhausted.");
  5556. }
  5557.  
  5558.  
  5559. char *
  5560. xmalloc (size)
  5561.      int size;
  5562. {
  5563.   extern char *malloc ();
  5564.   register char *ptr = malloc (size);
  5565.   if (ptr != 0) return (ptr);
  5566.   memory_full ();
  5567.   /*NOTREACHED*/
  5568. }
  5569.  
  5570. char *
  5571. xrealloc (old, size)
  5572.      char *old;
  5573.      int size;
  5574. {
  5575.   extern char *realloc ();
  5576.   register char *ptr = realloc (old, size);
  5577.   if (ptr != 0) return (ptr);
  5578.   memory_full ();
  5579.   /*NOTREACHED*/
  5580. }
  5581.  
  5582. char *
  5583. xcalloc (number, size)
  5584.      int number, size;
  5585. {
  5586.   extern char *malloc ();
  5587.   register int total = number * size;
  5588.   register char *ptr = malloc (total);
  5589.   if (ptr != 0) {
  5590.     if (total > 100)
  5591.       bzero (ptr, total);
  5592.     else {
  5593.       /* It's not too long, so loop, zeroing by longs.
  5594.      It must be safe because malloc values are always well aligned.  */
  5595.       register long *zp = (long *) ptr;
  5596.       register long *zl = (long *) (ptr + total - 4);
  5597.       register int i = total - 4;
  5598.       while (zp < zl)
  5599.     *zp++ = 0;
  5600.       if (i < 0)
  5601.     i = 0;
  5602.       while (i < total)
  5603.     ptr[i++] = 0;
  5604.     }
  5605.     return ptr;
  5606.   }
  5607.   memory_full ();
  5608.   /*NOTREACHED*/
  5609. }
  5610.  
  5611. char *
  5612. savestring (input)
  5613.      char *input;
  5614. {
  5615.   int size = strlen (input);
  5616.   char *output = xmalloc (size + 1);
  5617.   strcpy (output, input);
  5618.   return output;
  5619. }
  5620.  
  5621. /* Get the file-mode and data size of the file open on FD
  5622.    and store them in *MODE_POINTER and *SIZE_POINTER.  */
  5623.  
  5624. int
  5625. file_size_and_mode (fd, mode_pointer, size_pointer)
  5626.      int fd;
  5627.      int *mode_pointer;
  5628.      long int *size_pointer;
  5629. {
  5630.   struct stat sbuf;
  5631.  
  5632.   if (fstat (fd, &sbuf) < 0) return (-1);
  5633.   if (mode_pointer) *mode_pointer = sbuf.st_mode;
  5634.   if (size_pointer) *size_pointer = sbuf.st_size;
  5635.   return 0;
  5636. }
  5637.  
  5638. #ifdef    VMS
  5639.  
  5640. /* Under VMS we need to fix up the "include" specification
  5641.    filename so that everything following the 1st slash is
  5642.    changed into its correct VMS file specification. */
  5643.  
  5644. hack_vms_include_specification (fname)
  5645.      char *fname;
  5646. {
  5647.   register char *cp, *cp1, *cp2;
  5648.   char Local[512];
  5649.   extern char *index (), *rindex ();
  5650.  
  5651.   /* Ignore leading "./"s */
  5652.   while (fname[0] == '.' && fname[1] == '/')
  5653.     strcpy (fname, fname+2);
  5654.   /* Look for the boundary between the VMS and UNIX filespecs */
  5655.   cp = rindex (fname, ']');    /* Look for end of dirspec. */
  5656.   if (cp == 0) cp == rindex (fname, '>'); /* ... Ditto            */
  5657.   if (cp == 0) cp == rindex (fname, ':'); /* Look for end of devspec. */
  5658.   if (cp) {
  5659.     cp++;
  5660.   } else {
  5661.     cp = index (fname, '/');    /* Look for the "/" */
  5662.   }
  5663.  
  5664.   cp2 = Local; /* initialize */
  5665.  
  5666.   /* We are trying to do a number of things here.  First of all, we are 
  5667.      trying to hammer the filenames into a standard format, such that later 
  5668.      processing can handle them.
  5669.  
  5670.      If the file name contains something like [dir.], then it recognizes this
  5671.      as a root, and strips the ".]".  Later processing will add whatever is 
  5672.      needed to get things working properly.
  5673.  
  5674.      If no device is specified, then the first directory name is taken to be
  5675.      a device name (or a rooted logical).  */
  5676.  
  5677.   /* See if we found that 1st slash */
  5678.   if (cp == 0) return;        /* Nothing to do!!! */
  5679.   if (*cp != '/') return;    /* Nothing to do!!! */
  5680.   /* Point to the UNIX filename part (which needs to be fixed!) */
  5681.   cp1 = cp+1;
  5682.   /* If the directory spec is not rooted, we can just copy
  5683.      the UNIX filename part and we are done */
  5684.   if (((cp - fname) > 2) && ((cp[-1] == ']') || (cp[-1] == '>'))) {
  5685.     if (cp[-2] != '.') {
  5686. /*
  5687.  * The VMS part ends in a "]", where the preceeding character is not a ".".
  5688.  * We assume that the rest of the specification is correct, and we splice
  5689.  * the two parts together, and go back.  Actually, given the default
  5690.  * locations for include files listed in cccp.c, we will never get here.
  5691.  * Things could change, so we leave it in...
  5692.  */
  5693.         strcpy (cp, cp1);    /* Non-rooted */
  5694.         return;             /* trailing "]/", and not ".]/" */
  5695.     } else {
  5696. /* 
  5697.  * The VMS part has a ".]" at the end, and this will not do.  Later 
  5698.  * processing will add a second directory spec, and this would be a syntax 
  5699.  * error.  Thus we strip the ".]", and thus merge the directory specs.
  5700.  * We also backspace cp1, so that it points to a '/'.  This inhibits the
  5701.  * generation of the 000000 root directory spec (which does not belong here
  5702.  * in this case).
  5703.  */
  5704.         cp -= 2;        /* Strip ".]" */
  5705.         cp1--; };        /* backspace */
  5706.    } else {
  5707.  
  5708. /* We drop in here if there is no VMS style directory specification yet.
  5709.  * If there is no device specification either, we make the first dir a
  5710.  * device and try that.  If we do not do this, then we will be essentially
  5711.  * searching the users default directory (as if they did a #include "asdf.h").
  5712.  *
  5713.  * Then all we need to do is to push a '[' into the output string.  Later
  5714.  * processing will fill this in, and close the bracket.
  5715.  */
  5716.      if (cp[-1] != ':')        /* dev not in spec.  take first dir */
  5717.        *cp2++ = ':';
  5718.      *cp2++ = '[';        /* Open the directory specification */
  5719.    }
  5720.  
  5721.  /* at this point we assume that we have the device spec, and (at least
  5722.     the opening "[" for a directory specification.  We may have directories
  5723.     specified already */
  5724.  
  5725.   /* If there are no other slashes then the filename will be
  5726.      in the "root" directory.  Otherwise, we need to add
  5727.      directory specifications. */
  5728.   if (index (cp1, '/') == 0) {
  5729.       /* Just add "000000]" as the directory string */
  5730.       strcpy (cp2, "000000]");
  5731.       cp2 += strlen (cp2);
  5732.   } else {
  5733.       /* As long as there are still subdirectories to add, do them. */
  5734.       while (index (cp1, '/') != 0) {
  5735.       /* If this token is "." we can ignore it */
  5736.       if ((cp1[0] == '.') && (cp1[1] == '/')) {
  5737.           cp1 += 2;
  5738.           continue;
  5739.       }
  5740.       /* Add a subdirectory spec. Do not duplicate "." */
  5741.       if (cp2[-1] != '.' && cp2[-1] != '[' && cp2[-1] != '<')
  5742.           *cp2++ = '.';
  5743.       /* If this is ".." then the spec becomes "-" */
  5744.       if ((cp1[0] == '.') && (cp1[1] == '.') && (cp[2] == '/')) {
  5745.           /* Add "-" and skip the ".." */
  5746.           *cp2++ = '-';
  5747.           cp1 += 3;
  5748.           continue;
  5749.       }
  5750.       /* Copy the subdirectory */
  5751.       while (*cp1 != '/') *cp2++= *cp1++;
  5752.       cp1++;        /* Skip the "/" */
  5753.       }
  5754.       /* Close the directory specification */
  5755.       if(cp2[-1] == '.')    /* no trailing periods */
  5756.       cp2--;
  5757.       *cp2++ = ']';
  5758.   }
  5759.   /* Now add the filename */
  5760.   while (*cp1) *cp2++ = *cp1++;
  5761.   *cp2 = 0;
  5762.   /* Now append it to the original VMS spec. */
  5763.   strcpy (cp, Local);
  5764.   return;
  5765. }
  5766. #endif    /* VMS */
  5767.  
  5768. #ifdef    VMS
  5769.  
  5770. /* These are the read/write replacement routines for
  5771.    VAX-11 "C".  They make read/write behave enough
  5772.    like their UNIX counterparts that CCCP will work */
  5773.  
  5774. int
  5775. read (fd, buf, size)
  5776.      int fd;
  5777.      char *buf;
  5778.      int size;
  5779. {
  5780. #undef    read    /* Get back the REAL read routine */
  5781.   register int i;
  5782.   register int total = 0;
  5783.  
  5784.   /* Read until the buffer is exhausted */
  5785.   while (size > 0) {
  5786.     /* Limit each read to 32KB */
  5787.     i = (size > (32*1024)) ? (32*1024) : size;
  5788.     i = read (fd, buf, i);
  5789.     if (i <= 0) {
  5790.       if (i == 0) return (total);
  5791.       return(i);
  5792.     }
  5793.     /* Account for this read */
  5794.     total += i;
  5795.     buf += i;
  5796.     size -= i;
  5797.   }
  5798.   return (total);
  5799. }
  5800.  
  5801. int
  5802. write (fd, buf, size)
  5803.      int fd;
  5804.      char *buf;
  5805.      int size;
  5806. {
  5807. #undef    write    /* Get back the REAL write routine */
  5808.   int i;
  5809.   int j;
  5810.  
  5811.   /* Limit individual writes to 32Kb */
  5812.   i = size;
  5813.   while (i > 0) {
  5814.     j = (i > (32*1024)) ? (32*1024) : i;
  5815.     if (write (fd, buf, j) < 0) return (-1);
  5816.     /* Account for the data written */
  5817.     buf += j;
  5818.     i -= j;
  5819.   }
  5820.   return (size);
  5821. }
  5822.  
  5823. #endif /* VMS */
  5824. #ifdef amigados
  5825. nindex (str, ch, len)
  5826.      char *str;
  5827.      char ch;
  5828.      int len;
  5829. {
  5830.   while (*str && *str != ch && len) str++, len--;
  5831.   return *str == ch ? str : 0;
  5832. }
  5833. #endif
  5834.